-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplay_it.c
More file actions
62 lines (56 loc) · 1.79 KB
/
play_it.c
File metadata and controls
62 lines (56 loc) · 1.79 KB
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
#include<stdio.h>
#include<alsa/asoundlib.h>
int main()
{
int err,rate = 8000,i = 0,c;
int buf[128];
FILE *afile;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
size_t nread;
char* dir = "aud/";
char* extension = ".wav";
char afilename[strlen(dir)+strlen(extension)+3];
// open Audio 0[first] Card & 0[first] Device
if((err = snd_pcm_open(&pcm_handle,"plughw:0,0",SND_PCM_STREAM_PLAYBACK,0)) < 0)
{
fprintf(stderr,"Can't open the sound device %s",snd_strerror(err));
return err;
}
// setting parameters for ALSA
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle,params);
snd_pcm_hw_params_set_access(pcm_handle,params,SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(pcm_handle,params,SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(pcm_handle,params,1);
snd_pcm_hw_params_set_rate_near(pcm_handle,params,&rate,NULL);
if((err = snd_pcm_hw_params(pcm_handle,params)) < 0)
{
fprintf(stderr,"Can't set parameters for sound %s\n",snd_strerror(err));
return err;
}
// count and concatenate to make files
for (i; i<100;i++)
{
snprintf(afilename, sizeof( afilename ), "%s%d%s", dir, i, extension );
// opening file & proceed
afile = fopen(afilename, "r");
if(afile == NULL)
{
fprintf(stderr,"Can't Open file for reading\n");
return 1;
}
// read from afile [the wav file/s] 128 data elements each of which is 2 byte long and send to ALSA playback
while ((nread = fread(buf, sizeof(short), 128, afile)) > 0)
{
if((err = snd_pcm_writei(pcm_handle, buf, nread)) != nread)
{
fprintf(stderr,"Failed to write audio %s", snd_strerror(err));
snd_pcm_prepare(pcm_handle);
}
}
}
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
return 0;
}