-
Notifications
You must be signed in to change notification settings - Fork 6
/
sdlplay.c
250 lines (186 loc) · 3.25 KB
/
sdlplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <signal.h>
#include <stdio.h>
#include <SDL.h>
#include "pmdmini.h"
#include "pmdwinimport.h"
int audio_on = 0;
//
// PCM definitions ( BLOCK = samples )
//
#define PCM_BLOCK 512
#define PCM_BYTE_PER_SAMPLE 2
#define PCM_CH 2
//
// buffer size definitions
//
#define BUF_BLKSIZE PCM_BLOCK
#define BUF_BLKNUM 16
#define BUF_LEN ( BUF_BLKSIZE * PCM_CH * BUF_BLKNUM )
//
// audio buffering
//
int buf_wpos = 0;
int buf_ppos = 0;
short buffer[ BUF_LEN ];
static void audio_callback( void *param , Uint8 *data , int len )
{
int i;
short *pcm = (short *)data;
if ( !audio_on )
{
memset( data , 0 , len );
return;
}
for( i = 0; i < len / 2; i++ )
{
pcm[ i ] = buffer[ buf_ppos++ ];
if ( buf_ppos >= BUF_LEN )
buf_ppos = 0;
}
}
//
// audio hardware functions
//
static int init_audio(void)
{
SDL_AudioSpec af;
if ( SDL_Init( SDL_INIT_AUDIO ) )
{
printf("Failed to Initialize!!\n");
return 1;
}
af.freq = 44100;
af.format = AUDIO_S16;
af.channels = 2;
af.samples = 512;
af.callback = audio_callback;
af.userdata = NULL;
if (SDL_OpenAudio(&af,NULL) < 0)
{
printf("Audio Error!!\n");
return 1;
}
memset(buffer,0,sizeof(buffer));
SDL_PauseAudio(0);
return 0;
}
static void free_audio(void)
{
SDL_CloseAudio();
}
static void player_screen( void )
{
int i,n;
int notes[32];
n = pmd_get_tracks ( );
// 画面の制限
if (n > 8) n = 8;
pmd_get_current_notes( notes , n );
for ( i = 0; i < n; i ++ )
{
printf("%02x " , ( notes[i] & 0xff ) );
}
printf(" ");
}
//
// audio renderer
//
static void player_loop( int len )
{
int total;
int sec,old_sec;
int sec_sample;
int freq;
freq = 44100;
old_sec = total = sec = sec_sample = 0;
do
{
//
// Wait empty block
//
while( buf_wpos >= buf_ppos &&
buf_wpos < buf_ppos + ( PCM_BLOCK * PCM_CH ) )
{
SDL_Delay(10);
}
pmd_renderer ( buffer + buf_wpos, PCM_BLOCK );
buf_wpos += PCM_BLOCK * PCM_CH;
if (buf_wpos >= BUF_LEN)
buf_wpos = 0;
total += PCM_BLOCK;
sec_sample += PCM_BLOCK;
while ( sec_sample >= freq )
{
sec_sample -= freq;
sec++;
}
if ( sec != old_sec )
{
old_sec = sec;
}
player_screen();
printf("Time : %02d:%02d / %02d:%02d\r\r",
sec / 60 , sec % 60 , len / 60,len % 60);
fflush(stdout);
} while(sec < len);
}
//
// path splitter
//
static int split_dir( const char *file , char *dir )
{
const char *p;
int len = 0;
p = strrchr( file , '/' );
if ( p )
{
len = (int)( p - file );
strncpy ( dir , file , len );
}
dir[ len ] = 0;
return len;
}
static void audio_sig_handle(int sig)
{
pmd_stop();
free_audio();
exit(0);
}
//
// entry point
//
int main ( int argc, char *argv[] )
{
printf( "PMDPLAY on SDL Version 2022-07-26\n" );
if ( argc < 2 )
{
printf("Usage pmdplay <file>\n");
return 1;
}
if ( init_audio() )
{
return 1;
}
pmd_init();
signal(SIGINT, audio_sig_handle);
char buf[1024];
char *pcmdir = getenv( "HOME" );
if (pcmdir)
{
strcpy(buf,pcmdir);
strcat(buf,"/.pmdplay/");
}
else
buf[0] = 0;
if ( pmd_play( argv[1] , buf ) )
{
printf("File open error\n");
free_audio();
return 1;
}
audio_on = 1;
player_loop( pmd_length_sec() );
pmd_stop();
free_audio();
return 0;
}