-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalsa_play_sound.h
330 lines (268 loc) · 7.67 KB
/
alsa_play_sound.h
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Playing Wav using Alsa Sound Library
// Copyright Haris Hasanudin 2011
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#include <alsa/asoundlib.h>
typedef struct
{
u_int32_t dwSize ;
u_int16_t wFormatTag ;
u_int16_t wChannels ;
u_int32_t dwSamplesPerSec ;
u_int32_t dwAvgBytesPerSec ;
u_int16_t wBlockAlign ;
u_int16_t wBitsPerSample ;
} WAVEFORMAT ;
#if WORDS_BIGENDIAN
#define SwapLE16(x) ((((u_int16_t)x)<<8)|(((u_int16_t)x)>>8))
#define SwapLE32(x) ((((u_int32_t)x)<<24)|((((u_int32_t)x)<<8)&0x00FF0000) \
|((((u_int32_t)x)>>8)&0x0000FF00)|(((u_int32_t)x)>>24))
#else
#define SwapLE16(x) (x)
#define SwapLE32(x) (x)
#endif
#define BUFF_SIZE_SND_ALSA 1024
int alsa_play_son(const char * nom_fic_a_jouer);
void * alsa_play_BGM( void * arg )
{
TH_BOX * arg_bgm = (TH_BOX * ) arg;
char *fname=NULL;
fname = arg_bgm->myfile;
// printf("inside: %s\n", arg_bgm->myfile);
pthread_detach ( pthread_self() );
alsa_play_son( arg_bgm->myfile );
//printf( "\nselesai.\n" );
if(arg_bgm != NULL)
free (arg_bgm);
return 0;
}
char * alsa_find_chunk(char* pstart,const char* fourcc, size_t n)
{
char *pend ;
int k,test;
pend=pstart + n ;
while (pstart < pend)
{
if (*pstart == *fourcc) /* found match for first char*/
{
test = TRUE ;
for (k = 1 ; fourcc [k] != 0 ; k++)
test = (test ? ( pstart [k] == fourcc [k] ) : FALSE) ;
if (test)
return pstart ;
} ; /* if*/
pstart ++ ;
} ; /* while lpstart*/
return NULL ;
}
int alsa_init_son(FILE * fic_son,const char * nom_fic_a_jouer,
snd_pcm_t ** adr_handle,snd_pcm_uframes_t * adr_buffer_size,
size_t * adr_bits_per_frame,u_long * adr_datastart)
{
char buffer[BUFF_SIZE_SND_ALSA];
//size_t chunk_bytes;
char *device ; /* playback device */
//snd_pcm_uframes_t chunk_size;
size_t bits_per_sample;
WAVEFORMAT waveformat ;
u_long samples;
char * ptr;
u_long databytes;
snd_pcm_format_t format;
snd_pcm_hw_params_t * params;
int err;
// a partager
snd_pcm_t *handle;
snd_pcm_uframes_t buffer_size;
size_t bits_per_frame;
u_long datastart;
//device = strdup("plughw:0,0"); /* playback device */
device=strdup("default"); /* playback device */
if ( fseek(fic_son,0,SEEK_SET) != 0 )
{
printf("Salah, Rewinding WAV file %s\n",nom_fic_a_jouer);
return 101; /* Wav file must be seekable device */
}
fread(buffer,BUFF_SIZE_SND_ALSA,1,fic_son) ;
if (alsa_find_chunk(buffer,"RIFF",BUFF_SIZE_SND_ALSA) != buffer)
{
fprintf(stdout,"Salah format: tidak ditemukan RIFF file marker\n"); // wwg Report error
return 102;
}
if (! alsa_find_chunk(buffer,"WAVE",BUFF_SIZE_SND_ALSA))
{
fprintf(stdout,"Salah format: tidak ditemukan WAVE file marker\n");
return 103;
}
ptr=alsa_find_chunk(buffer, "fmt ",BUFF_SIZE_SND_ALSA) ;
if (! ptr)
{
fprintf(stdout,"Salah format: tidak ditemukan 'fmt' file marker\n"); /* wwg:report error */
return 104;
}
ptr+=4 ; /* Move past "fmt ".*/
memcpy (&waveformat, ptr, sizeof (WAVEFORMAT)) ;
waveformat.dwSize = SwapLE32(waveformat.dwSize);
waveformat.wFormatTag = SwapLE16(waveformat.wFormatTag) ;
waveformat.wChannels = SwapLE16(waveformat.wChannels) ;
waveformat.dwSamplesPerSec = SwapLE32(waveformat.dwSamplesPerSec) ;
waveformat.dwAvgBytesPerSec = SwapLE32(waveformat.dwAvgBytesPerSec) ;
waveformat.wBlockAlign = SwapLE16(waveformat.wBlockAlign) ;
waveformat.wBitsPerSample = SwapLE16(waveformat.wBitsPerSample) ;
ptr=alsa_find_chunk(buffer,"data",BUFF_SIZE_SND_ALSA) ;
if (! ptr)
{
fprintf(stdout,"Salah format: tidak ditemukan 'data' file marker\n"); /* wwg:report error */
return 105;
}
ptr+=4 ; /* Move past "data".*/
memcpy(&databytes, ptr, sizeof (u_long)) ;
samples = databytes / waveformat.wBlockAlign ;
datastart = ((u_long) (ptr + 4)) - ((u_long) (&(buffer[0]))) ;
switch (waveformat.wBitsPerSample)
{
case 8:
format=SND_PCM_FORMAT_U8 ;
break;
case 16:
format=SND_PCM_FORMAT_S16_LE ;
break;
case 32 :
format=SND_PCM_FORMAT_S32_LE;
break;
default :
fprintf(stdout,"Salah format: %i bits perseconds\n",waveformat.wBitsPerSample ); // wwg: report error
return 106;
break;
}
snd_pcm_hw_params_alloca(¶ms);
if ((err=snd_pcm_open (&handle, device,SND_PCM_STREAM_PLAYBACK,SND_PCM_ASYNC)) < 0)
{
fprintf(stdout, "tidak bisa buka audio device %s (%s)\n",device,snd_strerror (err));
return 107;
}
if ((err=snd_pcm_nonblock(handle, 1))< 0)
{
fprintf(stdout,"nonblock setting error: %s", snd_strerror(err));
return 108;
}
/* Init hwparams with full configuration space */
if (snd_pcm_hw_params_any(handle, params)<0)
{
fprintf(stdout, "tidak bisa konfigurasi PCM device.\n");
return 109;
}
err=snd_pcm_hw_params_set_access(handle,params,SND_PCM_ACCESS_RW_INTERLEAVED);
if (err<0)
{
fprintf(stdout,"tidak tersedia Access type");
return 110;
}
err=snd_pcm_hw_params_set_format(handle, params, format);
if (err < 0)
{
fprintf(stdout,"tidak tersedia Sample format");
return 111;
}
err=snd_pcm_hw_params_set_channels(handle,params,waveformat.wChannels);
if (err < 0)
{
fprintf(stdout,"tidak tersedia Channels count");
return 112;
}
err=snd_pcm_hw_params_set_rate_near(handle,params,&waveformat.dwSamplesPerSec, 0);
if (err < 0)
{
fprintf(stdout,"Unable to set rate : %d",waveformat.dwSamplesPerSec);
return 113;
}
assert(err >= 0);
err=snd_pcm_hw_params(handle,params);
if (err < 0)
{
fprintf(stdout,"tidak bisa instal hw params:");
return 114;
}
//chunk_size = 0;
buffer_size=0;
snd_pcm_hw_params_get_buffer_size(params,&buffer_size);
//snd_pcm_hw_params_get_period_size(params,&chunk_size,0);
bits_per_sample=snd_pcm_format_physical_width(format);
bits_per_frame=bits_per_sample*waveformat.wChannels;
//chunk_bytes=chunk_size*bits_per_frame/8;
*adr_handle=handle;
*adr_buffer_size=buffer_size;
*adr_bits_per_frame=bits_per_frame;
*adr_datastart=datastart;
return 0;
}
int alsa_play_son(const char * nom_fic_a_jouer)
{
int err;
FILE * fic_son;
snd_pcm_sframes_t nb_frames_envoyes;
int octects_lus,nb_frames_de_envoi,frames_rest_a_envoyer;
char *buffer2;
char * pos_buffer;
int err_init,nb_octets_par_frame;
// a partager
snd_pcm_t *handle;
snd_pcm_uframes_t buffer_size;
size_t bits_per_frame;
u_long datastart;
fic_son=fopen(nom_fic_a_jouer,"rb");
if (!fic_son)
{
fprintf(stdout,"Gagal buka file WAV %s\n",nom_fic_a_jouer);
return 10;
}
err_init=alsa_init_son(fic_son,nom_fic_a_jouer,&handle,&buffer_size,&bits_per_frame,&datastart);
if (err_init)
{
fclose(fic_son);
return err_init;
}
err=fseek(fic_son,datastart,SEEK_SET);
if (buffer_size>1024*64)
buffer_size=1024*64;
buffer2=(char *)malloc(buffer_size);
if (buffer2==0)
{
fprintf(stdout,"Salah Malloc in function : play_le_son\n");
return 11;
}
nb_octets_par_frame=bits_per_frame/8;
if (nb_octets_par_frame<1)
nb_octets_par_frame=1;
while ((octects_lus=fread(buffer2,1,buffer_size,fic_son)))
{
pos_buffer=buffer2;
nb_frames_de_envoi=octects_lus/nb_octets_par_frame;
frames_rest_a_envoyer=nb_frames_de_envoi;
nb_frames_envoyes=snd_pcm_writei(handle,pos_buffer,frames_rest_a_envoyer);
if (nb_frames_envoyes>0)
{
frames_rest_a_envoyer-=nb_frames_envoyes;
pos_buffer+=nb_octets_par_frame*nb_frames_envoyes;
}
while (frames_rest_a_envoyer>0)
{
nb_frames_envoyes=snd_pcm_writei(handle,pos_buffer,frames_rest_a_envoyer);
if (nb_frames_envoyes>0)
{
frames_rest_a_envoyer-=nb_frames_envoyes;
pos_buffer+=nb_octets_par_frame*nb_frames_envoyes;
}
if (nb_frames_envoyes==-EPIPE) // une erreur est survenue
frames_rest_a_envoyer=0;
}
}
free(buffer2);
fclose(fic_son);
snd_pcm_drain(handle);
return 0;
}