1 |
/*************************************************************************** |
---|
2 |
Copyright (C) 2005 by Pieter Palmers * |
---|
3 |
* |
---|
4 |
This program is free software; you can redistribute it and/or modify * |
---|
5 |
it under the terms of the GNU General Public License as published by * |
---|
6 |
the Free Software Foundation; either version 2 of the License, or * |
---|
7 |
(at your option) any later version. * |
---|
8 |
* |
---|
9 |
This program is distributed in the hope that it will be useful, * |
---|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
12 |
GNU General Public License for more details. * |
---|
13 |
* |
---|
14 |
You should have received a copy of the GNU General Public License * |
---|
15 |
along with this program; if not, write to the * |
---|
16 |
Free Software Foundation, Inc., * |
---|
17 |
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
---|
18 |
***************************************************************************/ |
---|
19 |
|
---|
20 |
|
---|
21 |
/** |
---|
22 |
* Test application for the direct decode stream API |
---|
23 |
* for floating point use |
---|
24 |
*/ |
---|
25 |
|
---|
26 |
#ifdef HAVE_CONFIG_H |
---|
27 |
#include <config.h> |
---|
28 |
#endif |
---|
29 |
|
---|
30 |
#include <stdio.h> |
---|
31 |
#include <stdlib.h> |
---|
32 |
#include <string.h> |
---|
33 |
|
---|
34 |
#include <signal.h> |
---|
35 |
|
---|
36 |
#include "libfreebob/freebob_streaming.h" |
---|
37 |
|
---|
38 |
#include "debugtools.h" |
---|
39 |
|
---|
40 |
int run; |
---|
41 |
|
---|
42 |
static void sighandler (int sig) |
---|
43 |
{ |
---|
44 |
run = 0; |
---|
45 |
} |
---|
46 |
|
---|
47 |
int main(int argc, char *argv[]) |
---|
48 |
{ |
---|
49 |
|
---|
50 |
#define PERIOD_SIZE 256 |
---|
51 |
|
---|
52 |
int samplesread=0; |
---|
53 |
// int sampleswritten=0; |
---|
54 |
int nb_in_channels=0, nb_out_channels=0; |
---|
55 |
int retval=0; |
---|
56 |
int i=0; |
---|
57 |
|
---|
58 |
int nb_periods=0; |
---|
59 |
|
---|
60 |
float **audiobuffers_in; |
---|
61 |
freebob_sample_t **audiobuffers_out; |
---|
62 |
freebob_sample_t *nullbuffer; |
---|
63 |
|
---|
64 |
run=1; |
---|
65 |
|
---|
66 |
printf("Freebob streaming test application (3)\n"); |
---|
67 |
|
---|
68 |
signal (SIGINT, sighandler); |
---|
69 |
signal (SIGPIPE, sighandler); |
---|
70 |
|
---|
71 |
freebob_device_info_t device_info; |
---|
72 |
|
---|
73 |
freebob_options_t dev_options; |
---|
74 |
|
---|
75 |
dev_options.sample_rate=44100; |
---|
76 |
dev_options.period_size=PERIOD_SIZE; |
---|
77 |
|
---|
78 |
dev_options.nb_buffers=3; |
---|
79 |
|
---|
80 |
dev_options.port=0; |
---|
81 |
dev_options.node_id=-1; |
---|
82 |
|
---|
83 |
dev_options.realtime=1; |
---|
84 |
dev_options.packetizer_priority=70; |
---|
85 |
|
---|
86 |
freebob_device_t *dev=freebob_streaming_init(&device_info, dev_options); |
---|
87 |
if (!dev) { |
---|
88 |
fprintf(stderr,"Could not init Freebob Streaming layer\n"); |
---|
89 |
exit(-1); |
---|
90 |
} |
---|
91 |
|
---|
92 |
nb_in_channels=freebob_streaming_get_nb_capture_streams(dev); |
---|
93 |
nb_out_channels=freebob_streaming_get_nb_playback_streams(dev); |
---|
94 |
|
---|
95 |
/* allocate intermediate buffers */ |
---|
96 |
audiobuffers_in=calloc(nb_in_channels,sizeof(float *)); |
---|
97 |
audiobuffers_out=calloc(nb_in_channels,sizeof(freebob_sample_t)); |
---|
98 |
for (i=0;i<nb_in_channels;i++) { |
---|
99 |
audiobuffers_in[i]=calloc(PERIOD_SIZE+1,sizeof(float)); |
---|
100 |
audiobuffers_out[i]=calloc(PERIOD_SIZE+1,sizeof(freebob_sample_t)); |
---|
101 |
|
---|
102 |
switch (freebob_streaming_get_capture_stream_type(dev,i)) { |
---|
103 |
case freebob_stream_type_audio: |
---|
104 |
/* assign the audiobuffer to the stream */ |
---|
105 |
freebob_streaming_set_capture_stream_buffer(dev, i, (char *)(audiobuffers_in[i])); |
---|
106 |
freebob_streaming_set_capture_buffer_type(dev, i, freebob_buffer_type_float); |
---|
107 |
break; |
---|
108 |
// this is done with read/write routines because the nb of bytes can differ. |
---|
109 |
case freebob_stream_type_midi: |
---|
110 |
default: |
---|
111 |
break; |
---|
112 |
} |
---|
113 |
} |
---|
114 |
|
---|
115 |
nullbuffer=calloc(PERIOD_SIZE+1,sizeof(freebob_sample_t)); |
---|
116 |
|
---|
117 |
for (i=0;i<nb_out_channels;i++) { |
---|
118 |
switch (freebob_streaming_get_playback_stream_type(dev,i)) { |
---|
119 |
case freebob_stream_type_audio: |
---|
120 |
if (i<nb_in_channels) { |
---|
121 |
/* assign the audiobuffer to the stream */ |
---|
122 |
freebob_streaming_set_playback_stream_buffer(dev, i, (char *)(audiobuffers_in[i])); |
---|
123 |
freebob_streaming_set_playback_buffer_type(dev, i, freebob_buffer_type_float); |
---|
124 |
} else { |
---|
125 |
freebob_streaming_set_playback_stream_buffer(dev, i, (char *)nullbuffer); |
---|
126 |
freebob_streaming_set_playback_buffer_type(dev, i, freebob_buffer_type_int24); |
---|
127 |
} |
---|
128 |
break; |
---|
129 |
// this is done with read/write routines because the nb of bytes can differ. |
---|
130 |
case freebob_stream_type_midi: |
---|
131 |
default: |
---|
132 |
break; |
---|
133 |
} |
---|
134 |
} |
---|
135 |
|
---|
136 |
/* open the files to write to*/ |
---|
137 |
FILE* fid_out[nb_out_channels]; |
---|
138 |
FILE* fid_in[nb_in_channels]; |
---|
139 |
char name[256]; |
---|
140 |
|
---|
141 |
for (i=0;i<nb_out_channels;i++) { |
---|
142 |
snprintf(name,sizeof(name),"out_ch_%02d",i); |
---|
143 |
|
---|
144 |
fid_out[i]=fopen(name,"w"); |
---|
145 |
|
---|
146 |
freebob_streaming_get_playback_stream_name(dev,i,name,sizeof(name)); |
---|
147 |
fprintf(fid_out[i],"Channel name: %s\n",name); |
---|
148 |
switch (freebob_streaming_get_playback_stream_type(dev,i)) { |
---|
149 |
case freebob_stream_type_audio: |
---|
150 |
fprintf(fid_out[i],"Channel type: audio\n"); |
---|
151 |
break; |
---|
152 |
case freebob_stream_type_midi: |
---|
153 |
fprintf(fid_out[i],"Channel type: midi\n"); |
---|
154 |
break; |
---|
155 |
case freebob_stream_type_unknown: |
---|
156 |
fprintf(fid_out[i],"Channel type: unknown\n"); |
---|
157 |
break; |
---|
158 |
default: |
---|
159 |
case freebob_stream_type_invalid: |
---|
160 |
fprintf(fid_out[i],"Channel type: invalid\n"); |
---|
161 |
break; |
---|
162 |
} |
---|
163 |
|
---|
164 |
} |
---|
165 |
for (i=0;i<nb_in_channels;i++) { |
---|
166 |
snprintf(name,sizeof(name),"in_ch_%02d",i); |
---|
167 |
fid_in[i]=fopen(name,"w"); |
---|
168 |
|
---|
169 |
freebob_streaming_get_capture_stream_name(dev,i,name,sizeof(name)); |
---|
170 |
fprintf(fid_in[i], "Channel name: %s\n"); |
---|
171 |
switch (freebob_streaming_get_capture_stream_type(dev,i)) { |
---|
172 |
case freebob_stream_type_audio: |
---|
173 |
fprintf(fid_in[i], "Channel type: audio\n"); |
---|
174 |
break; |
---|
175 |
case freebob_stream_type_midi: |
---|
176 |
fprintf(fid_in[i], "Channel type: midi\n"); |
---|
177 |
break; |
---|
178 |
case freebob_stream_type_unknown: |
---|
179 |
fprintf(fid_in[i],"Channel type: unknown\n"); |
---|
180 |
break; |
---|
181 |
default: |
---|
182 |
case freebob_stream_type_invalid: |
---|
183 |
fprintf(fid_in[i],"Channel type: invalid\n"); |
---|
184 |
break; |
---|
185 |
} |
---|
186 |
} |
---|
187 |
|
---|
188 |
// start the streaming layer |
---|
189 |
freebob_streaming_start(dev); |
---|
190 |
|
---|
191 |
fprintf(stderr,"Entering receive loop (%d,%d)\n",nb_in_channels,nb_out_channels); |
---|
192 |
while(run) { |
---|
193 |
retval = freebob_streaming_wait(dev); |
---|
194 |
if (retval < 0) { |
---|
195 |
fprintf(stderr,"Xrun\n"); |
---|
196 |
freebob_streaming_reset(dev); |
---|
197 |
continue; |
---|
198 |
} |
---|
199 |
|
---|
200 |
// freebob_streaming_transfer_buffers(dev); |
---|
201 |
freebob_streaming_transfer_capture_buffers(dev); |
---|
202 |
freebob_streaming_transfer_playback_buffers(dev); |
---|
203 |
|
---|
204 |
nb_periods++; |
---|
205 |
|
---|
206 |
if((nb_periods % 32)==0) { |
---|
207 |
// fprintf(stderr,"\r%05d periods",nb_periods); |
---|
208 |
} |
---|
209 |
|
---|
210 |
for(i=0;i<nb_in_channels;i++) { |
---|
211 |
|
---|
212 |
|
---|
213 |
switch (freebob_streaming_get_capture_stream_type(dev,i)) { |
---|
214 |
case freebob_stream_type_audio: |
---|
215 |
// no need to get the buffers manually, we have set the API internal buffers to the audiobuffer[i]'s |
---|
216 |
// //samplesread=freebob_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE); |
---|
217 |
samplesread=PERIOD_SIZE; |
---|
218 |
break; |
---|
219 |
case freebob_stream_type_midi: |
---|
220 |
samplesread=freebob_streaming_read(dev, i, audiobuffers_out[i], PERIOD_SIZE); |
---|
221 |
break; |
---|
222 |
} |
---|
223 |
|
---|
224 |
// fprintf(fid_in[i], "---- Period read (%d samples) ----\n",samplesread); |
---|
225 |
// hexDumpToFile(fid_in[i],(unsigned char*)audiobuffers_in[i],samplesread*sizeof(float)+1); |
---|
226 |
} |
---|
227 |
|
---|
228 |
for(i=0;i<nb_out_channels;i++) { |
---|
229 |
freebob_sample_t *buff; |
---|
230 |
int sampleswritten=0; |
---|
231 |
if (i<nb_in_channels) { |
---|
232 |
buff=audiobuffers_out[i]; |
---|
233 |
} else { |
---|
234 |
buff=nullbuffer; |
---|
235 |
} |
---|
236 |
|
---|
237 |
switch (freebob_streaming_get_playback_stream_type(dev,i)) { |
---|
238 |
case freebob_stream_type_audio: |
---|
239 |
// sampleswritten=freebob_streaming_write(dev, i, buff, PERIOD_SIZE); |
---|
240 |
sampleswritten=PERIOD_SIZE; |
---|
241 |
break; |
---|
242 |
case freebob_stream_type_midi: |
---|
243 |
// sampleswritten=freebob_streaming_write(dev, i, buff, PERIOD_SIZE); |
---|
244 |
break; |
---|
245 |
} |
---|
246 |
// fprintf(fid_out[i], "---- Period write (%d samples) ----\n",sampleswritten); |
---|
247 |
// hexDumpToFile(fid_out[i],(unsigned char*)buff,sampleswritten*sizeof(freebob_sample_t)); |
---|
248 |
} |
---|
249 |
|
---|
250 |
} |
---|
251 |
|
---|
252 |
fprintf(stderr,"\n"); |
---|
253 |
|
---|
254 |
fprintf(stderr,"Exiting receive loop\n"); |
---|
255 |
|
---|
256 |
freebob_streaming_stop(dev); |
---|
257 |
|
---|
258 |
freebob_streaming_finish(dev); |
---|
259 |
|
---|
260 |
for (i=0;i<nb_out_channels;i++) { |
---|
261 |
fclose(fid_out[i]); |
---|
262 |
|
---|
263 |
} |
---|
264 |
for (i=0;i<nb_in_channels;i++) { |
---|
265 |
fclose(fid_in[i]); |
---|
266 |
} |
---|
267 |
|
---|
268 |
for (i=0;i<nb_in_channels;i++) { |
---|
269 |
free(audiobuffers_in[i]); |
---|
270 |
free(audiobuffers_out[i]); |
---|
271 |
} |
---|
272 |
free(nullbuffer); |
---|
273 |
free(audiobuffers_in); |
---|
274 |
free(audiobuffers_out); |
---|
275 |
|
---|
276 |
return EXIT_SUCCESS; |
---|
277 |
} |
---|