root/trunk/libffado/tests/streaming/teststreaming2.c

Revision 445, 7.7 kB (checked in by pieterpalmers, 15 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

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