/
alsatest.c
309 lines (181 loc) · 5.7 KB
/
alsatest.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
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
//i<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<USING MMAP>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
#include<stdio.h>
#include<alsa/asoundlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/time.h>
int main()
{
const snd_pcm_channel_area_t *areas;
int size;
unsigned char *buffer;
char *pcm_name;
unsigned int rate = 8000;
int chan;
int rc;
unsigned int val;
char check;
int pcmreturn;
//Set PCM stream and handle
snd_pcm_t *pcm_handle;
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
//Set HARDWARE parameters
snd_pcm_hw_params_t *hwparams;
//pcm_name = (char *)malloc(10);
pcm_name = "plughw:0,0";
//allocate memory for hardware
if(snd_pcm_hw_params_malloc(&hwparams)<0)
{
fprintf(stderr,"No memory allocated for hardware");
return(-1);
}
if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) {
fprintf(stderr, "Error opening PCM device %s\n", pcm_name);
return(-1);
}
//INITIALIZE HARDWARE WITH CONFIGURATION OF THE SOUNDCARD
if(snd_pcm_hw_params_any(pcm_handle,hwparams)<0)
{
fprintf(stderr,"The hardware device cannot be configured\n");
}
//SET FORMAT TO 16 BIT LITTLE ENDIAN
snd_pcm_hw_params_set_format(pcm_handle,hwparams,SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(pcm_handle,hwparams,2);
/* 44100 bits/second sampling rate (CD quality) */
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
fprintf(stderr, "Error setting access.\n");
return(-1);
}
//SETTING RATE ie SAMPLING FREQUENCY
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, 0);
//SETTING HARDWARE PARAMETERS
if(rc=(snd_pcm_hw_params(pcm_handle,hwparams))<0)
{
fprintf(stderr,"\nCannot set hardware parameters\n");
return -1;
}
//snd_pcm_uframes_t periodsize = 8192;
snd_pcm_uframes_t frames,offset ;//OF TYPE UNSIGNED LONG
snd_pcm_sframes_t commitres;
//frames =32;
//SETTING SOME MORE PARAMETERS
//size = frames*4;
//SETTING BUFFER SIZE
/*if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (periodsize*2)>>2) < 0) {
fprintf(stderr, "Error setting buffersize.\n");
return(-1);
}
*/
int first=0;;
int err;
snd_pcm_sframes_t avail = snd_pcm_avail_update(pcm_handle);
printf("\nNumber of frames available is : %d \n",(int)avail);
//-------------------------------------------------------------------------------------------------------
//<<<<<<<<<<<<<<<<<<<<<<DISPLAYING INFORMATION>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Buffer size
snd_pcm_hw_params_get_buffer_size(hwparams, (snd_pcm_uframes_t*)&val);
printf("\nThe buffer size is %d \n",val);
//Buffer time
snd_pcm_hw_params_get_buffer_time(hwparams, &val,0);
printf("\nBuffer time is : %d \n",val);
//Period size
snd_pcm_hw_params_get_period_size(hwparams,&frames,0);
printf("\nperiod size : %d \n",(int)frames);
////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//<<<<<<<<<<<<<mmap_begin>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
err=snd_pcm_mmap_begin(pcm_handle,&areas,&offset,&frames);
//Periods between buffer
snd_pcm_hw_params_get_periods(hwparams,&val,0);
printf("\n Periods between buffer is : %d \n",val);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
if(err<0)
{
printf("\nMMAP error\n");
return -1;
}
else printf("\n%d\n",err);
/*
commitres = snd_pcm_mmap_commit(pcm_handle,offset,frames);
if(commitres<0)
{
printf("\nFrames not committed to memory\n");
}
*/
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,OPENING FILE>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....
int dest = open("sample3.wav",O_RDONLY);
if(dest<0)
{
perror("\nFile could not be opened\n");
return -1;
}
//buffer = (unsigned char *)malloc(1000);
/*
unsigned long buf = offset;
if(read(dest,&offset,1)<0)
{
perror("\nNo destination for read\n");
return -1;
}
*/
/*
avail=snd_pcm_avail_update(pcm_handle);
printf("\nAvailable frames = %d \n\n",(int)avail);
struct timeval tvbefore,tvafter;
gettimeofday(&tvbefore,NULL);
*/
unsigned char *ptr[2];
int i;
//unsigned int steps=0;
for(i=0;i<2;i++)
ptr[i] = (unsigned char*)areas[i].addr + (areas[i].first/8) + offset*(areas[i].step/8);
printf("\nOffset of the first sample : %u \n",areas[0].first);
printf("\n\nOffset of the memory map is : %u \n",areas[0].step/8);
printf("\nAreas start address is : %u \n",areas[0].addr);
printf("\nNumber of frames ; %u \n",frames);
printf("\n\nPointer before ; %u \n",ptr[0]);
long pl;
for(pl=0;pl<100000;pl++);
if(ptr[1]==NULL)
printf("\nNull pointer 1 allocated\n");
if(ptr[0]==NULL)
printf("\nNull pointer 0 allocated \n");
int check2=1;
snd_pcm_sframes_t size2 =30;
while(1)
{
for(i=0;i<2;i++)
{
//ptr[i] = (unsigned char*)areas[i].addr + (areas[i].first/8) + offset*(areas[i].step/8);
if(read(dest,ptr[i],120)!=0)
{
if(ptr[i]!=NULL)
{
if(pcmreturn = snd_pcm_mmap_writei(pcm_handle,ptr[i],size2)<0)
{
snd_pcm_prepare(pcm_handle);
printf("\n<<<<<<<Buffer Underrun>>>>>>>>\n");
break;
}
// printf("\n%d\n",pcmreturn);
ptr[i]+=0;
printf("\n%lu\n",ptr[i]);
}
}
else {
check2=0;
break;
}
}
if(check2==0)
break;
// l:break;
}
commitres = snd_pcm_mmap_commit(pcm_handle,offset,frames);
if(commitres<0)
{
printf("\nFrames not committed to memory\n");
}
}