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

Revision 742, 7.6 kB (checked in by ppalmers, 13 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2007 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  * This program 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 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
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 "libffado/ffado.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         int start_flag=0;
61
62         int nb_periods=0;
63
64         ffado_sample_t **audiobuffer;
65         ffado_sample_t *nullbuffer;
66        
67         run=1;
68
69         printf("Ffado streaming test application (2)\n");
70
71         signal (SIGINT, sighandler);
72         signal (SIGPIPE, sighandler);
73
74         ffado_device_info_t device_info;
75
76         ffado_options_t dev_options;
77
78         dev_options.sample_rate=44100;
79         dev_options.period_size=PERIOD_SIZE;
80
81         dev_options.nb_buffers=3;
82
83         dev_options.realtime=0;
84         dev_options.packetizer_priority=60;
85        
86         dev_options.verbose=5;
87        
88         dev_options.slave_mode=0;
89         dev_options.snoop_mode=0;
90
91         ffado_device_t *dev=ffado_streaming_init(device_info, dev_options);
92         if (!dev) {
93                 fprintf(stderr,"Could not init Ffado Streaming layer\n");
94                 exit(-1);
95         }
96
97         nb_in_channels=ffado_streaming_get_nb_capture_streams(dev);
98         nb_out_channels=ffado_streaming_get_nb_playback_streams(dev);
99
100         /* allocate intermediate buffers */
101         audiobuffer=calloc(nb_in_channels,sizeof(ffado_sample_t *));
102         for (i=0;i<nb_in_channels;i++) {
103                 audiobuffer[i]=calloc(PERIOD_SIZE+1,sizeof(ffado_sample_t));
104                        
105                 switch (ffado_streaming_get_capture_stream_type(dev,i)) {
106                         case ffado_stream_type_audio:
107                                 /* assign the audiobuffer to the stream */
108                                 ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(audiobuffer[i]));
109                                 ffado_streaming_set_capture_buffer_type(dev, i, ffado_buffer_type_int24);
110                                 break;
111                                 // this is done with read/write routines because the nb of bytes can differ.
112                         case ffado_stream_type_midi:
113                         default:
114                                 break;
115                 }
116         }
117        
118         nullbuffer=calloc(PERIOD_SIZE+1,sizeof(ffado_sample_t));
119
120 #if 1
121         for (i=0;i<nb_out_channels;i++) {
122                 switch (ffado_streaming_get_capture_stream_type(dev,i)) {
123                         case ffado_stream_type_audio:
124                                 if (i<nb_in_channels) {
125                                         /* assign the audiobuffer to the stream */
126                                         ffado_streaming_set_playback_stream_buffer(dev, i, (char *)audiobuffer[i]);
127                                 } else {
128                                         ffado_streaming_set_playback_stream_buffer(dev, i, (char *)nullbuffer);
129                                 }
130                                 ffado_streaming_set_playback_buffer_type(dev, i, ffado_buffer_type_int24);
131                                 break;
132                                 // this is done with read/write routines because the nb of bytes can differ.
133                         case ffado_stream_type_midi:
134                         default:
135                                 break;
136                 }
137         }
138 #endif
139        
140         /* open the files to write to*/
141         FILE* fid_out[nb_out_channels];
142         FILE* fid_in[nb_in_channels];
143         char name[256];
144
145         for (i=0;i<nb_out_channels;i++) {
146                 snprintf(name,sizeof(name),"out_ch_%02d",i);
147
148                 fid_out[i]=fopen(name,"w");
149
150                 ffado_streaming_get_playback_stream_name(dev,i,name,sizeof(name));
151                 fprintf(fid_out[i],"Channel name: %s\n",name);
152                 switch (ffado_streaming_get_playback_stream_type(dev,i)) {
153                 case ffado_stream_type_audio:
154                         fprintf(fid_out[i],"Channel type: audio\n");
155                         break;
156                 case ffado_stream_type_midi:
157                         fprintf(fid_out[i],"Channel type: midi\n");
158                         break;
159                 case ffado_stream_type_unknown:
160                         fprintf(fid_out[i],"Channel type: unknown\n");
161                         break;
162                 default:
163                 case ffado_stream_type_invalid:
164                         fprintf(fid_out[i],"Channel type: invalid\n");
165                         break;
166                 }
167
168         }
169         for (i=0;i<nb_in_channels;i++) {
170                 snprintf(name,sizeof(name),"in_ch_%02d",i);
171                 fid_in[i]=fopen(name,"w");
172
173                 ffado_streaming_get_capture_stream_name(dev,i,name,sizeof(name));
174                 fprintf(fid_in[i], "Channel name: %s\n",name);
175                 switch (ffado_streaming_get_capture_stream_type(dev,i)) {
176                 case ffado_stream_type_audio:
177                         fprintf(fid_in[i], "Channel type: audio\n");
178                         break;
179                 case ffado_stream_type_midi:
180                         fprintf(fid_in[i], "Channel type: midi\n");
181                         break;
182                 case ffado_stream_type_unknown:
183                         fprintf(fid_in[i],"Channel type: unknown\n");
184                         break;
185                 default:
186                 case ffado_stream_type_invalid:
187                         fprintf(fid_in[i],"Channel type: invalid\n");
188                         break;
189                 }
190         }
191
192 FILE *of=fopen("foo.dat","w");
193
194         // prepare and start the streaming layer
195         ffado_streaming_prepare(dev);
196         start_flag = ffado_streaming_start(dev);
197
198         fprintf(stderr,"Entering receive loop (%d,%d)\n",nb_in_channels,nb_out_channels);
199         while(run && start_flag==0) {
200                 retval = ffado_streaming_wait(dev);
201                 if (retval < 0) {
202                         fprintf(stderr,"Xrun\n");
203                         ffado_streaming_reset(dev);
204                         continue;
205                 }
206                
207                 for (i=0;i<nb_in_channels;i++) {
208                         memset(audiobuffer[i],0xCC,(PERIOD_SIZE+1)*sizeof(ffado_sample_t));
209                 }
210
211 //              ffado_streaming_transfer_buffers(dev);
212                 ffado_streaming_transfer_capture_buffers(dev);
213                 ffado_streaming_transfer_playback_buffers(dev);
214                
215                 nb_periods++;
216
217                 if((nb_periods % 32)==0) {
218                         fprintf(stderr,"\r%05d periods",nb_periods);
219                 }
220
221                 for(i=0;i<nb_in_channels;i++) {
222                        
223                        
224                         switch (ffado_streaming_get_capture_stream_type(dev,i)) {
225                         case ffado_stream_type_audio:
226                                 // no need to get the buffers manually, we have set the API internal buffers to the audiobuffer[i]'s
227 //                              //samplesread=ffado_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE);
228                                 samplesread=PERIOD_SIZE;
229                                 break;
230                         case ffado_stream_type_midi:
231                                 samplesread=ffado_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE);
232                                 break;
233                         default:
234                                 ;
235                         }
236        
237 //                      fprintf(fid_in[i], "---- Period read  (%d samples) ----\n",samplesread);
238 //                      hexDumpToFile(fid_in[i],(unsigned char*)audiobuffer[i],samplesread*sizeof(ffado_sample_t)+1);
239 // FIXME: Dump analog1 as raw data to a separate binary file for testing
240 //if (i==2) {
241 //  fwrite(audiobuffer[i],sizeof(ffado_sample_t),samplesread,of);
242 //}
243                 }
244
245                 for(i=0;i<nb_out_channels;i++) {
246                         ffado_sample_t *buff;
247                         if (i<nb_in_channels) {
248                                 buff=audiobuffer[i];
249                         } else {
250                                 buff=nullbuffer;
251                         }
252                        
253                         switch (ffado_streaming_get_playback_stream_type(dev,i)) {
254                         case ffado_stream_type_audio:
255 //// Calling ffado_streaming_write() causes problems since the buffer is external here.
256 //// Just mirror the read case since it seems to work.
257 ////                            sampleswritten=ffado_streaming_write(dev, i, buff, PERIOD_SIZE);
258                                 sampleswritten=PERIOD_SIZE;
259                                 break;
260                         case ffado_stream_type_midi:
261                                 sampleswritten=ffado_streaming_write(dev, i, buff, PERIOD_SIZE);
262                                 break;
263                         default:
264                                 ;
265                         }
266 //                      fprintf(fid_out[i], "---- Period write (%d samples) ----\n",sampleswritten);
267 //                      hexDumpToFile(fid_out[i],(unsigned char*)buff,sampleswritten*sizeof(ffado_sample_t));
268                 }
269
270         }
271
272         fprintf(stderr,"\n");
273
274         fprintf(stderr,"Exiting receive loop\n");
275        
276         ffado_streaming_stop(dev);
277
278         ffado_streaming_finish(dev);
279         fclose(of);
280
281         for (i=0;i<nb_out_channels;i++) {
282                 fclose(fid_out[i]);
283
284         }
285         for (i=0;i<nb_in_channels;i++) {
286                 fclose(fid_in[i]);
287         }
288        
289         for (i=0;i<nb_in_channels;i++) {
290                 free(audiobuffer[i]);
291         }
292         free(nullbuffer);
293         free(audiobuffer);
294
295   return EXIT_SUCCESS;
296 }
Note: See TracBrowser for help on using the browser.