DSP.xs

filter: Statementstmt 14.6% 54 / 368 Branchbran 8.3% 18 / 216 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 12.3% 72 / 584 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Audio/DSP.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <sys/soundcard.h>
10  
11 #define AUDIO_FILE_BUFFER_SIZE 4096
12  
13 static int
14 0 not_here(char *s)
15 {
16 0 croak("%s not implemented on this architecture", s);
17 return -1;
18 }
19  
20 static double
21 1 constant(char *name, int arg)
22 {
23 1 errno = 0;
24 1 ▶ switch (*name) {
Branch: 1 branch
BranchTrueFalseTotal
switch (*name) { 0 1 1
25 0 case 'A':
26 0 ▶ if (strEQ(name, "AFMT_A_LAW"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_A_LAW")) 0 0 0
27 #ifdef AFMT_A_LAW
28 0 return AFMT_A_LAW;
29 #else
30 goto not_there;
31 #endif
32 0 ▶ if (strEQ(name, "AFMT_IMA_ADPCM"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_IMA_ADPCM")) 0 0 0
33 #ifdef AFMT_IMA_ADPCM
34 0 return AFMT_IMA_ADPCM;
35 #else
36 goto not_there;
37 #endif
38 0 ▶ if (strEQ(name, "AFMT_MPEG"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_MPEG")) 0 0 0
39 #ifdef AFMT_MPEG
40 0 return AFMT_MPEG;
41 #else
42 goto not_there;
43 #endif
44 0 ▶ if (strEQ(name, "AFMT_MU_LAW"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_MU_LAW")) 0 0 0
45 #ifdef AFMT_MU_LAW
46 0 return AFMT_MU_LAW;
47 #else
48 goto not_there;
49 #endif
50 0 ▶ if (strEQ(name, "AFMT_QUERY"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_QUERY")) 0 0 0
51 #ifdef AFMT_QUERY
52 0 return AFMT_QUERY;
53 #else
54 goto not_there;
55 #endif
56 0 ▶ if (strEQ(name, "AFMT_S16_BE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_S16_BE")) 0 0 0
57 #ifdef AFMT_S16_BE
58 0 return AFMT_S16_BE;
59 #else
60 goto not_there;
61 #endif
62 0 ▶ if (strEQ(name, "AFMT_S16_LE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_S16_LE")) 0 0 0
63 #ifdef AFMT_S16_LE
64 0 return AFMT_S16_LE;
65 #else
66 goto not_there;
67 #endif
68 0 ▶ if (strEQ(name, "AFMT_S16_NE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_S16_NE")) 0 0 0
69 #ifdef AFMT_S16_NE
70 0 return AFMT_S16_NE;
71 #else
72 goto not_there;
73 #endif
74 0 ▶ if (strEQ(name, "AFMT_S8"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_S8")) 0 0 0
75 #ifdef AFMT_S8
76 0 return AFMT_S8;
77 #else
78 goto not_there;
79 #endif
80 0 ▶ if (strEQ(name, "AFMT_U16_BE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_U16_BE")) 0 0 0
81 #ifdef AFMT_U16_BE
82 0 return AFMT_U16_BE;
83 #else
84 goto not_there;
85 #endif
86 0 ▶ if (strEQ(name, "AFMT_U16_LE"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_U16_LE")) 0 0 0
87 #ifdef AFMT_U16_LE
88 0 return AFMT_U16_LE;
89 #else
90 goto not_there;
91 #endif
92 0 ▶ if (strEQ(name, "AFMT_U8"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "AFMT_U8")) 0 0 0
93 #ifdef AFMT_U8
94 0 return AFMT_U8;
95 #else
96 goto not_there;
97 #endif
98 0 break;
99 }
100 1 errno = EINVAL;
101 1 return 0;
102  
103 not_there:
104 errno = ENOENT;
105 return 0;
106 }
107  
108 1 int _audioformat (SV* fmt) {
109 char* val;
110  
111 /* format specified as integer */
112 1 ▶ if (SvIOK(fmt))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(fmt)) 1 0 1
113 1 return (SvIV(fmt));
114  
115 /* constants are recognized as
116 * floating-point numbers
117 */
118 0 ▶ else if (SvNOK(fmt))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(fmt)) 0 0 0
119 0 return (int)SvNV(fmt);
120  
121 /* format specified as string - DEPRECATED */
122 0 ▶ else if (SvPOK(fmt)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(fmt)) { 0 0 0
123 0 val = SvPVX(fmt);
124  
125 0 ▶ if (strEQ(val, "AFMT_QUERY"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(val, "AFMT_QUERY")) 0 0 0
126 0 return(AFMT_QUERY);
127 0 ▶ else if (strEQ(val, "AFMT_MU_LAW"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_MU_LAW")) 0 0 0
128 0 return(AFMT_MU_LAW);
129 0 ▶ else if (strEQ(val, "AFMT_A_LAW"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_A_LAW")) 0 0 0
130 0 return(AFMT_A_LAW);
131 0 ▶ else if (strEQ(val, "AFMT_IMA_ADPCM"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_IMA_ADPCM")) 0 0 0
132 0 return(AFMT_IMA_ADPCM);
133 0 ▶ else if (strEQ(val, "AFMT_U8"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_U8")) 0 0 0
134 0 return(AFMT_U8);
135 0 ▶ else if (strEQ(val, "AFMT_S16_LE"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_S16_LE")) 0 0 0
136 0 return(AFMT_S16_LE);
137 0 ▶ else if (strEQ(val, "AFMT_S16_BE"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_S16_BE")) 0 0 0
138 0 return(AFMT_S16_BE);
139 0 ▶ else if (strEQ(val, "AFMT_S8"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_S8")) 0 0 0
140 0 return(AFMT_S8);
141 0 ▶ else if (strEQ(val, "AFMT_U16_LE"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_U16_LE")) 0 0 0
142 0 return(AFMT_U16_LE);
143 0 ▶ else if (strEQ(val, "AFMT_U16_BE"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_U16_BE")) 0 0 0
144 0 return(AFMT_U16_BE);
145 0 ▶ else if (strEQ(val, "AFMT_MPEG"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "AFMT_MPEG")) 0 0 0
146 0 return(AFMT_MPEG);
147 else {
148 0 return(-1);
149 }
150 } else {
151 0 return(-1);
152 }
153 }
154  
155 0 int _modeflag (SV* flag) {
156 int mode;
157 char* val;
158  
159 /* mode specified as integer */
160 0 ▶ if (SvIOK(flag))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(flag)) 0 0 0
161 0 mode = SvIV(flag);
162  
163 /* Fcntl.pm-exported constants are recognized as
164 * floating-point numbers
165 */
166 0 ▶ else if (SvNOK(flag))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(flag)) 0 0 0
167 0 return (int)SvNV(flag);
168  
169 /* mode specified as string - DEPRECATED */
170 0 ▶ else if (SvPOK(flag)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(flag)) { 0 0 0
171 0 val = SvPVX(flag);
172 0 ▶ if (strEQ(val, "O_RDONLY"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(val, "O_RDONLY")) 0 0 0
173 0 mode = O_RDONLY;
174 0 ▶ else if (strEQ(val, "O_WRONLY"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "O_WRONLY")) 0 0 0
175 0 mode = O_WRONLY;
176 0 ▶ else if (strEQ(val, "O_RDWR"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(val, "O_RDWR")) 0 0 0
177 0 mode = O_RDWR;
178 else
179 0 mode = -1;
180  
181 } else {
182 0 mode = -1;
183 }
184 0 return(mode);
185 }
186  
187 MODULE = Audio::DSP PACKAGE = Audio::DSP
188  
189 PROTOTYPES: ENABLED
190  
191 double
192 constant(name,arg)
193 char * name
194 int arg
195  
196 #############################################
197 ################ Constructor ################
198  
199 void
200 new (...)
201 PPCODE:
202 {
203 1 HV* construct = newHV();
204 1 HV* thistash = newHV();
205  
206 1 SV* buff = newSViv(4096); /* read/write buffer size */
207 1 SV* chan = newSViv(1); /* mono(1) or stereo(2) */
208 1 SV* data = newSVpv("",0); /* stored audio data */
209 1 SV* device = newSVpv("/dev/dsp",8);
210 1 SV* errstr = newSVpvf("",0);
211 1 SV* file_indicator = newSViv(0); /* file descriptor */
212 1 SV* format = newSViv(AFMT_U8); /* 8 bit unsigned is default */
213 1 SV* mark = newSViv(0); /* play position */
214 1 SV* rate = newSViv(8192); /* sampling rate */
215 SV* self;
216  
217 char audio_buff[AUDIO_FILE_BUFFER_SIZE];
218 char* audio_file; /* if "file" param exists */
219 char* key; /* param name */
220  
221 int audio_fd;
222 int status;
223 int stidx; /* stack index */
224  
225 /******** get parameters ********/
226 5 ▶ for (stidx = items % 2; stidx < items; stidx += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (stidx = items % 2; stidx < items; stidx += 2) { 4 1 5
227 4 key = SvPVX(ST(stidx));
228  
229 4 ▶ if (strEQ(key, "device"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "device")) 0 4 4
230 0 sv_setpv(device, SvPVX(ST(stidx + 1)));
231  
232 4 ▶ else if (strEQ(key, "buffer"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "buffer")) 1 3 4
233 1 sv_setiv(buff, SvIV(ST(stidx + 1)));
234  
235 3 ▶ else if (strEQ(key, "rate"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "rate")) 1 2 3
236 1 sv_setiv(rate, SvIV(ST(stidx + 1)));
237  
238 2 ▶ else if (strEQ(key, "format")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "format")) { 1 1 2
239 1 sv_setiv(format, _audioformat(ST(stidx + 1)));
240 1 ▶ if (SvIV(format) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(format) < 0) 0 1 1
241 0 croak("error determining audio format");
242 }
243  
244 1 ▶ else if (strEQ(key, "channels"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "channels")) 1 0 1
245 1 sv_setiv(chan, SvIV(ST(stidx + 1)));
246  
247 /**** store data from existing audio file ****/
248 0 ▶ else if (strEQ(key, "file")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "file")) { 0 0 0
249 0 audio_file = SvPVX(ST(stidx + 1));
250 0 audio_fd = open(audio_file, O_RDONLY);
251 0 ▶ if (audio_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (audio_fd < 0) 0 0 0
252 0 croak("failed to open %s", audio_file);
253 for (;;) {
254 0 ▶ status = read(audio_fd, audio_buff, AUDIO_FILE_BUFFER_SIZE); if (status == 0)
Branch: 1 branch
BranchTrueFalseTotal
status = read(audio_fd, audio_buff, AUDIO_FILE_BUFFER_SIZE); if (status == 0) 0 0 0
255 0 break;
256 else
257 0 sv_catpvn(data, audio_buff, status);
258 }
259 0 ▶ if (close(audio_fd) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (close(audio_fd) < 0) 0 0 0
260 0 croak("problem closing audio file %s", audio_file);
261 }
262 }
263  
264 /******** assign settings to new object ********/
265 1 hv_store(construct, "buffer", 6, buff, 0);
266 1 hv_store(construct, "channels", 8, chan, 0);
267 1 hv_store(construct, "data", 4, data, 0);
268 1 hv_store(construct, "device", 6, device, 0);
269 1 hv_store(construct, "errstr", 6, errstr, 0);
270 1 hv_store(construct, "file_indicator", 14, file_indicator, 0);
271 1 hv_store(construct, "format", 6, format, 0);
272 1 hv_store(construct, "mark", 4, mark, 0);
273 1 hv_store(construct, "rate", 4, rate, 0);
274  
275 1 self = newRV_inc((SV*)construct); /* make a reference */
276 1 thistash = gv_stashpv("Audio::DSP", 0);
277 1 sv_bless(self, thistash); /* bless it */
278 1 ▶ XPUSHs(self); /* push it */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); /* push it */ 0 1 1
279 }
280  
281 #################################################################
282 ############## Methods for opening / closing device #############
283  
284 void
285 init (...)
286 PPCODE:
287 {
288 SV* format;
289 1 HV* caller = (HV*)SvRV(ST(0));
290 char* dev;
291 char* key;
292 char* val;
293 int arg;
294 int fd;
295 1 int mode = O_RDWR; /* device open mode */
296 int status;
297 int stidx;
298  
299 1 ▶ if ((items % 2) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items % 2) == 0) 0 1 1
300 0 croak("Odd number of elements in hash list");
301  
302 1 ▶ for (stidx = 1; stidx < items; stidx += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (stidx = 1; stidx < items; stidx += 2) { 0 1 1
303 0 key = SvPVX(ST(stidx));
304  
305 0 ▶ if (strEQ(key, "device")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "device")) { 0 0 0
306 0 hv_store(caller, "device", 6, newSVsv(ST(stidx + 1)), 0);
307 0 ▶ } else if (strEQ(key, "buffer")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "buffer")) { 0 0 0
308 0 hv_store(caller, "buffer", 6, newSVsv(ST(stidx + 1)), 0);
309 0 ▶ } else if (strEQ(key, "channels")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "channels")) { 0 0 0
310 0 hv_store(caller, "channels", 8, newSVsv(ST(stidx + 1)), 0);
311 0 ▶ } else if (strEQ(key, "rate")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "rate")) { 0 0 0
312 0 hv_store(caller, "rate", 4, newSVsv(ST(stidx + 1)), 0);
313 0 ▶ } else if (strEQ(key, "format")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "format")) { 0 0 0
314 0 hv_store(caller, "format", 6,
315 newSViv(_audioformat(ST(stidx + 1))), 0);
316 0 ▶ if (SvIV(*hv_fetch(caller, "format", 6, 0)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(*hv_fetch(caller, "format", 6, 0)) < 0) { 0 0 0
317 0 hv_store(caller, "errstr", 6,
318 newSVpvf("error determining audio format"), 0);
319 0 XSRETURN_NO;
320 }
321 }
322  
323 /******** get file mode ********/
324 0 ▶ else if (strEQ(key, "mode")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "mode")) { 0 0 0
325 0 mode = _modeflag(ST(stidx + 1));
326 0 ▶ if (mode < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode < 0) { 0 0 0
327 0 hv_store(caller, "errstr", 6,
328 newSVpvf("failed to recognize open flag"), 0);
329 0 XSRETURN_NO;
330 }
331 }
332 }
333  
334 /**** device name ****/
335 1 dev = SvPVX(*hv_fetch(caller, "device", 6, 0));
336  
337 /**** open device ****/
338 1 fd = open(dev, mode);
339 1 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 1 0 1
340 1 hv_store(caller, "errstr", 6,
341 newSVpvf("failed to open device '%s'", dev), 0);
342 1 XSRETURN_NO;
343 }
344  
345 /**** set sampling format ****/
346 0 arg = SvIV(*hv_fetch(caller, "format", 6, 0));
347  
348 0 ▶ if (ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) { 0 0 0
349 0 hv_store(caller, "errstr", 6,
350 newSVpvf("SNDCTL_DSP_SETFMT ioctl failed"), 0);
351 0 XSRETURN_NO;
352 }
353 0 ▶ if (arg != SvIV(*hv_fetch(caller, "format", 6, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg != SvIV(*hv_fetch(caller, "format", 6, 0))) { 0 0 0
354 0 hv_store(caller, "errstr", 6,
355 newSVpvf("failed to set sample format"), 0);
356 0 XSRETURN_NO;
357 }
358  
359 /**** set channels ****/
360 0 arg = SvIV(*hv_fetch(caller, "channels", 8, 0));
361 0 ▶ if (ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1) { 0 0 0
362 0 hv_store(caller, "errstr", 6,
363 newSVpvf("SNDCTL_DSP_CHANNELS ioctl failed"), 0);
364 0 XSRETURN_NO;
365  
366 }
367 0 ▶ if (arg != SvIV(*hv_fetch(caller, "channels", 8, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg != SvIV(*hv_fetch(caller, "channels", 8, 0))) { 0 0 0
368 0 hv_store(caller, "errstr", 6,
369 newSVpvf("failed to set number of channels"), 0);
370 0 XSRETURN_NO;
371 }
372
373 /**** set sampling rate ****/
374 0 arg = SvIV(*hv_fetch(caller, "rate", 4, 0));
375 0 ▶ if (ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) { 0 0 0
376 0 hv_store(caller, "errstr", 6,
377 newSVpvf("SNDCTL_DSP_SPEED ioctl failed"), 0);
378 0 XSRETURN_NO;
379 }
380 0 ▶ if (arg != SvIV(*hv_fetch(caller, "rate", 4, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg != SvIV(*hv_fetch(caller, "rate", 4, 0))) { 0 0 0
381 0 hv_store(caller, "errstr", 6,
382 newSVpvf("failed to set sampling rate"), 0);
383 0 XSRETURN_NO;
384 }
385
386 /**** store file descriptor in Audio::DSP object ****/
387 0 hv_store(caller, "file_indicator", 14, newSViv(fd), 0);
388  
389 0 XSRETURN_YES;
390 }
391  
392 void
393 open (...)
394 PPCODE:
395 {
396 /* open without sending any ioctl messages */
397 0 HV* caller = (HV*)SvRV(ST(0));
398 SV* flag;
399 int fd;
400 0 int mode = O_RDWR;
401 0 char* dev = SvPVX(*hv_fetch(caller, "device", 6, 0));
402  
403 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
404 0 flag = ST(1);
405 0 mode = _modeflag(flag);
406 0 ▶ if (mode < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode < 0) { 0 0 0
407 0 hv_store(caller, "errstr", 6,
408 newSVpvf("unrecognized open flag"), 0);
409 0 XSRETURN_NO;
410 }
411 }
412 0 fd = open(dev, mode);
413 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
414 0 hv_store(caller, "errstr", 6,
415 newSVpvf("failed to open audio device file"), 0);
416 0 XSRETURN_NO;
417 }
418 0 hv_store(caller, "file_indicator", 14, newSViv(fd), 0);
419 0 XSRETURN_YES;
420 }
421  
422 void
423 close (...)
424 PPCODE:
425 {
426 /* fetch file descriptor and close... nothing fancy */
427 0 int fd = SvIV(*hv_fetch((HV*)SvRV(ST(0)), "file_indicator", 14, 0));
428  
429 0 ▶ if (close(fd) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (close(fd) < 0) 0 0 0
430 0 XSRETURN_NO;
431 else
432 0 XSRETURN_YES;
433 }
434  
435 #################################################################
436 ####################### I/O control methods #####################
437  
438 void
439 channels (...)
440 PPCODE:
441 {
442 0 HV* caller = (HV*)SvRV(ST(0));
443 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
444 int chan, arg;
445 0 chan = arg = SvIV(ST(1));
446  
447 0 ▶ if (ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1) { 0 0 0
448 0 hv_store(caller, "errstr", 6,
449 newSVpvf("SNDCTL_DSP_CHANNELS ioctl failed"), 0);
450 0 XSRETURN_NO;
451 }
452 0 ▶ XPUSHs(newSViv(arg));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSViv(arg)); 0 0 0
453 }
454  
455 void
456 getfmts (...)
457 PPCODE:
458 {
459 0 HV* caller = (HV*)SvRV(ST(0));
460 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
461 int mask;
462  
463 0 ▶ if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) { 0 0 0
464 0 hv_store(caller, "errstr", 6,
465 newSVpvf("SNDCTL_DSP_GETFMTS ioctl failed"), 0);
466 0 XSRETURN_NO;
467 }
468 0 ▶ XPUSHs(newSViv(mask));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSViv(mask)); 0 0 0
469 }
470  
471 void
472 post (...)
473 PPCODE:
474 {
475 0 HV* caller = (HV*)SvRV(ST(0));
476 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
477  
478 0 ▶ if (ioctl(fd, SNDCTL_DSP_POST, 0) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_POST, 0) == -1) { 0 0 0
479 0 hv_store(caller, "errstr", 6,
480 newSVpvf("SNDCTL_DSP_POST ioctl failed"), 0);
481 0 XSRETURN_NO;
482 }
483 0 XSRETURN_YES;
484 }
485  
486 void
487 reset (...)
488 PPCODE:
489 {
490 0 HV* caller = (HV*)SvRV(ST(0));
491 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
492  
493 0 ▶ if (ioctl(fd, SNDCTL_DSP_RESET, 0) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_RESET, 0) == -1) { 0 0 0
494 0 hv_store(caller, "errstr", 6,
495 newSVpvf("SNDCTL_DSP_RESET ioctl failed"), 0);
496 0 XSRETURN_NO;
497 }
498 0 XSRETURN_YES;
499 }
500  
501 void
502 setduplex (...)
503 PPCODE:
504 {
505 0 HV* caller = (HV*)SvRV(ST(0));
506 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
507  
508 0 ▶ if (ioctl(fd, SNDCTL_DSP_SETDUPLEX) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SETDUPLEX) == -1) { 0 0 0
509 0 hv_store(caller, "errstr", 6,
510 newSVpvf("SNDCTL_DSP_SETDUPLEX ioctl failed"), 0);
511 0 XSRETURN_NO;
512 }
513 0 XSRETURN_YES;
514 }
515  
516 void
517 setfmt (...)
518 PPCODE:
519 {
520 0 HV* caller = (HV*)SvRV(ST(0));
521 /* SV* format = ST(1); */
522 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
523 int fmt, arg;
524 /* fmt = arg = _audioformat(format); */
525 0 fmt = arg = SvIV(ST(1));
526  
527 0 ▶ if (ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) { 0 0 0
528 0 hv_store(caller, "errstr", 6,
529 newSVpvf("SNDCTL_DSP_SETFMT ioctl failed"), 0);
530 0 XSRETURN_NO;
531 }
532 0 ▶ XPUSHs(newSViv(arg));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSViv(arg)); 0 0 0
533 }
534  
535 void
536 speed (...)
537 PPCODE:
538 {
539 0 HV* caller = (HV*)SvRV(ST(0));
540 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
541 int rate, arg;
542 0 rate = arg = SvIV(ST(1));
543  
544 0 ▶ if (ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) { 0 0 0
545 0 hv_store(caller, "errstr", 6,
546 newSVpvf("SNDCTL_DSP_SPEED ioctl failed"), 0);
547 0 XSRETURN_NO;
548 }
549 0 ▶ XPUSHs(newSViv(arg));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSViv(arg)); 0 0 0
550 }
551  
552 void
553 sync (...)
554 PPCODE:
555 {
556 0 HV* caller = (HV*)SvRV(ST(0));
557 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
558  
559 0 ▶ if (ioctl(fd, SNDCTL_DSP_SYNC, 0) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_SYNC, 0) == -1) { 0 0 0
560 0 hv_store(caller, "errstr", 6,
561 newSVpvf("SNDCTL_DSP_SYNC ioctl failed"), 0);
562 0 XSRETURN_NO;
563 }
564 0 XSRETURN_YES;
565 }
566  
567 #################################################################
568 ##################### Direct device I/O #########################
569  
570 void
571 dread (...)
572 PPCODE:
573 {
574 /* read data and return it */
575 0 HV* caller = (HV*)SvRV(ST(0));
576 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
577  
578 int count;
579 int status;
580 char *buf;
581  
582 0 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 0 0 0
583 0 count = SvIV(ST(1));
584 else
585 0 count = SvIV(*hv_fetch(caller, "buffer", 6, 0));
586  
587 0 buf = malloc(count);
588 0 status = read(fd, buf, count);
589 0 ▶ if (status != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != count) { 0 0 0
590 0 hv_store(caller, "errstr", 6,
591 newSVpvf("failed to read correct number of bytes"), 0);
592 0 XSRETURN_NO;
593 }
594 0 ▶ XPUSHs(newSVpvn(buf, status));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSVpvn(buf, status)); 0 0 0
595 0 free(buf);
596 }
597  
598 void
599 dwrite (...)
600 PPCODE:
601 {
602 0 HV* caller = (HV*)SvRV(ST(0));
603 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
604  
605 int status;
606 0 int count = SvCUR(ST(1));
607 0 char* diy_data = SvPVX(ST(1));
608  
609 0 status = write(fd, diy_data, count);
610 0 ▶ if (status != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != count) { 0 0 0
611 0 hv_store(caller, "errstr", 6,
612 newSVpvf("failed to write correct number of bytes"), 0);
613 0 XSRETURN_NO;
614 }
615 0 XSRETURN_YES;
616 }
617  
618 #################################################################
619 ##################### "data-in-memory" methods ##################
620  
621 void
622 audiofile (...)
623 PPCODE:
624 {
625 0 HV* caller = (HV*)SvRV(ST(0));
626 char audio_buff[AUDIO_FILE_BUFFER_SIZE];
627 char* audio_file;
628 int audio_fd;
629 int status;
630  
631 0 audio_file = SvPVX(ST(1));
632 0 audio_fd = open(audio_file, O_RDONLY);
633  
634 0 ▶ if (audio_fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (audio_fd < 0) { 0 0 0
635 0 hv_store(caller, "errstr", 6,
636 newSVpvf("failed to open audio file '%s'", audio_file), 0);
637 0 XSRETURN_NO;
638 }
639  
640 for (;;) {
641 0 status = read(audio_fd, audio_buff, AUDIO_FILE_BUFFER_SIZE);
642 0 ▶ if (status == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) 0 0 0
643 0 break;
644 else
645 0 sv_catpvn(*hv_fetch(caller, "data", 4, 0), audio_buff, status);
646 }
647  
648 0 ▶ if (close(audio_fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (close(audio_fd) < 0) { 0 0 0
649 0 hv_store(caller, "errstr", 6,
650 newSVpvf("problem closing audio file '%s'", audio_file), 0);
651 0 XSRETURN_NO;
652 }
653 0 XSRETURN_YES;
654 }
655  
656 void
657 clear (...)
658 PPCODE:
659 {
660 0 HV* caller = (HV*)SvRV(ST(0));
661 0 hv_store(caller, "data", 4, newSVpv("",0), 0);
662 0 hv_store(caller, "mark", 4, newSViv(0), 0);
663 }
664  
665 void
666 data (...)
667 PPCODE:
668 {
669 0 ▶ XPUSHs(*hv_fetch((HV*)SvRV(ST(0)), "data", 4, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch((HV*)SvRV(ST(0)), "data", 4, 0)); 0 0 0
670 }
671  
672 void
673 datacat (...)
674 PPCODE:
675 {
676 0 HV* caller = (HV*)SvRV(ST(0));
677 0 int len = SvCUR(ST(1));
678  
679 0 sv_catpvn(*hv_fetch(caller, "data", 4, 0), SvPVX(ST(1)), len);
680 0 ▶ XPUSHs(sv_2mortal(newSViv(SvCUR(*hv_fetch(caller, "data", 4, 0)))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(SvCUR(*hv_fetch(caller, "data", 4, 0))))); 0 0 0
681 }
682  
683 void
684 datalen (...)
685 PPCODE:
686 {
687 0 ▶ XPUSHs(sv_2mortal(newSViv(SvCUR(*hv_fetch((HV*)SvRV(ST(0)), "data", 4, 0)))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(SvCUR(*hv_fetch((HV*)SvRV(ST(0)), "data", 4, 0))))); 0 0 0
688 }
689  
690 void
691 read (...)
692 PPCODE:
693 0 {
694 /* read one buffer length of data */
695 0 HV* caller = (HV*)SvRV(ST(0));
696 0 int count = SvIV(*hv_fetch(caller, "buffer", 6, 0));
697 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
698 int status;
699 0 char buf[count];
700  
701 0 status = read(fd, buf, count); /* record some sound */
702 0 ▶ if (status != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != count) { 0 0 0
703 0 hv_store(caller, "errstr", 6,
704 newSVpvf("failed to read correct number of bytes"), 0);
705 0 XSRETURN_NO;
706 }
707  
708 0 sv_catpvn(*hv_fetch(caller, "data", 4, 0), buf, status);
709 0 XSRETURN_YES;
710 }
711  
712 void
713 setmark (...)
714 PPCODE:
715 {
716 0 HV* caller = (HV*)SvRV(ST(0));
717 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
718 0 SvREFCNT_inc(ST(1));
719 0 hv_store(caller, "mark", 4, ST(1), 0);
720 }
721 0 ▶ XPUSHs(*hv_fetch(caller, "mark", 4, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "mark", 4, 0)); 0 0 0
722 }
723  
724 void
725 write (...)
726 PPCODE:
727 {
728 0 HV* caller = (HV*)SvRV(ST(0));
729 0 int count = SvIV(*hv_fetch(caller, "buffer", 6, 0));
730 0 int dlength = SvCUR(*hv_fetch(caller, "data", 4, 0));
731 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
732 0 int mark = SvIV(*hv_fetch(caller, "mark", 4, 0));
733 int status;
734 char* data;
735  
736 0 ▶ if (mark >= dlength) /* end of data */
Branch: 1 branch
BranchTrueFalseTotal
if (mark >= dlength) /* end of data */ 0 0 0
737 0 XSRETURN_NO;
738  
739 0 data = SvPVX(*hv_fetch(caller, "data", 4, 0));
740  
741 0 status = write(fd, &data[mark], count);
742  
743 /*** This just causes unnecessary problems...
744 * if (status != count) {
745 * hv_store(caller, "errstr", 6,
746 * newSVpvf("failed to write correct number of bytes"), 0);
747 * XSRETURN_NO;
748 * }
749 */
750  
751 0 hv_store(caller, "mark", 4, newSViv(mark + count), 0);
752 0 XSRETURN_YES;
753 }
754  
755 ####################################################################
756 ######################### misc. methods ############################
757  
758 void
759 errstr (...)
760 PPCODE:
761 {
762 1 ▶ XPUSHs(*hv_fetch((HV*)SvRV(ST(0)), "errstr", 6, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch((HV*)SvRV(ST(0)), "errstr", 6, 0)); 0 1 1
763 }
764  
765 ####################################################################
766 ################## Deprecated methods from v.0.01 ##################
767  
768 void
769 getformat (...)
770 PPCODE:
771 {
772 0 HV* caller = (HV*)SvRV(ST(0));
773 0 SV* format = ST(1);
774 0 int arg = _audioformat(format);
775 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
776 int mask;
777  
778 0 ▶ if (arg < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg < 0) { 0 0 0
779 0 hv_store(caller, "errstr", 6,
780 newSVpvf("error determining audio format"), 0);
781 0 XSRETURN_NO;
782 }
783  
784 0 ▶ if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) { 0 0 0
785 0 hv_store(caller, "errstr", 6,
786 newSVpvf("SNDCTL_DSP_GETFMTS ioctl failed"), 0);
787 0 XSRETURN_NO;
788 0 ▶ } else if (mask & arg) /* the format is supported */
Branch: 1 branch
BranchTrueFalseTotal
} else if (mask & arg) /* the format is supported */ 0 0 0
789 0 XSRETURN_YES;
790 else
791 0 hv_store(caller, "errstr", 6,
792 newSVpvf("format not supported"), 0);
793 0 XSRETURN_NO;
794 }
795  
796 void
797 queryformat (...)
798 PPCODE:
799 {
800 0 HV* caller = (HV*)SvRV(ST(0));
801 0 int fd = SvIV(*hv_fetch(caller, "file_indicator", 14, 0));
802 0 int status = ioctl(fd, SNDCTL_DSP_SETFMT, AFMT_QUERY);
803 0 ▶ XPUSHs(newSViv(status));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSViv(status)); 0 0 0
804 }
805  
806 void
807 setbuffer (...)
808 PPCODE:
809 {
810 0 HV* caller = (HV*)SvRV(ST(0));
811 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
812 0 SvREFCNT_inc(ST(1));
813 0 hv_store(caller, "buffer", 6, ST(1), 0);
814 }
815 0 ▶ XPUSHs(*hv_fetch(caller, "buffer", 6, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "buffer", 6, 0)); 0 0 0
816 }
817  
818 void
819 setchannels (...)
820 PPCODE:
821 {
822 0 HV* caller = (HV*)SvRV(ST(0));
823 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
824 0 SvREFCNT_inc(ST(1));
825 0 hv_store(caller, "channels", 8, ST(1), 0);
826 }
827 0 ▶ XPUSHs(*hv_fetch(caller, "channels", 8, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "channels", 8, 0)); 0 0 0
828 }
829  
830 void
831 setdevice (...)
832 PPCODE:
833 {
834 0 HV* caller = (HV*)SvRV(ST(0));
835 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
836 0 SvREFCNT_inc(ST(1));
837 0 hv_store(caller, "device", 6, ST(1), 0);
838 }
839 0 ▶ XPUSHs(*hv_fetch(caller, "device", 6, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "device", 6, 0)); 0 0 0
840 }
841  
842 void
843 setformat (...)
844 PPCODE:
845 {
846 0 HV* caller = (HV*)SvRV(ST(0));
847  
848 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
849 0 SvREFCNT_inc(ST(1));
850 0 hv_store(caller, "format", 6, newSViv(_audioformat(ST(1))), 0);
851 0 ▶ if (SvIV(*hv_fetch(caller, "format", 6, 0)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(*hv_fetch(caller, "format", 6, 0)) < 0) { 0 0 0
852 0 hv_store(caller, "errstr", 6,
853 newSVpvf("error determining audio format"), 0);
854 0 XSRETURN_NO;
855 }
856 }
857  
858 0 ▶ XPUSHs(*hv_fetch(caller, "format", 6, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "format", 6, 0)); 0 0 0
859 }
860  
861 void
862 setrate (...)
863 PPCODE:
864 {
865 0 HV* caller = (HV*)SvRV(ST(0));
866 0 ▶ if (items >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) { 0 0 0
867 0 SvREFCNT_inc(ST(1));
868 0 hv_store(caller, "rate", 4, ST(1), 0);
869 }
870 0 ▶ XPUSHs(*hv_fetch(caller, "rate", 4, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*hv_fetch(caller, "rate", 4, 0)); 0 0 0
871 }
« blib/lib/Audio/DSP.pm ↑ Summary