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

Revision 739, 7.8 kB (checked in by ppalmers, 15 years ago)

- Adapt the ffado external API (upgrade to v3)

NEEDS NEW JACK BACKEND

- simplify FFADODevice constructor even more
- implement first framework support for supporting multiple adapters.

currently all firewire adapters are scanned for supported devices unless specified otherwise
however attaching devices to separate adapters is not supported. using multiple adapters at
that are connected together might work.

  • 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  * for floating point use
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 256
55
56         int samplesread=0;
57 //      int sampleswritten=0;
58         int nb_in_channels=0, nb_out_channels=0;
59         int retval=0;
60         int i=0;
61         int start_flag = 0;
62
63         int nb_periods=0;
64
65         float **audiobuffers_in;
66         ffado_sample_t **audiobuffers_out;
67         ffado_sample_t *nullbuffer;
68        
69         run=1;
70
71         printf("Ffado streaming test application (3)\n");
72
73         signal (SIGINT, sighandler);
74         signal (SIGPIPE, sighandler);
75
76         ffado_device_info_t device_info;
77         memset(&device_info,0,sizeof(ffado_device_info_t));
78
79         ffado_options_t dev_options;
80         memset(&dev_options,0,sizeof(ffado_options_t));
81
82         dev_options.sample_rate=44100;
83         dev_options.period_size=PERIOD_SIZE;
84
85         dev_options.nb_buffers=3;
86
87         dev_options.realtime=1;
88         dev_options.packetizer_priority=70;
89        
90         dev_options.verbose=5;
91        
92         dev_options.slave_mode=0;
93         dev_options.snoop_mode=0;
94        
95         ffado_device_t *dev=ffado_streaming_init(device_info, dev_options);
96
97         if (!dev) {
98                 fprintf(stderr,"Could not init Ffado Streaming layer\n");
99                 exit(-1);
100         }
101
102         nb_in_channels=ffado_streaming_get_nb_capture_streams(dev);
103         nb_out_channels=ffado_streaming_get_nb_playback_streams(dev);
104
105         /* allocate intermediate buffers */
106         audiobuffers_in=calloc(nb_in_channels,sizeof(float *));
107         audiobuffers_out=calloc(nb_in_channels,sizeof(ffado_sample_t));
108         for (i=0;i<nb_in_channels;i++) {
109                 audiobuffers_in[i]=calloc(PERIOD_SIZE+1,sizeof(float));
110                 audiobuffers_out[i]=calloc(PERIOD_SIZE+1,sizeof(ffado_sample_t));
111                        
112                 switch (ffado_streaming_get_capture_stream_type(dev,i)) {
113                         case ffado_stream_type_audio:
114                                 /* assign the audiobuffer to the stream */
115                                 ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(audiobuffers_in[i]));
116                                 ffado_streaming_set_capture_buffer_type(dev, i, ffado_buffer_type_float);
117                                 ffado_streaming_playback_stream_onoff(dev, i, 1);
118                                 break;
119                                 // this is done with read/write routines because the nb of bytes can differ.
120                         case ffado_stream_type_midi:
121                         default:
122                                 break;
123                 }
124         }
125        
126         nullbuffer=calloc(PERIOD_SIZE+1,sizeof(ffado_sample_t));
127        
128         for (i=0;i<nb_out_channels;i++) {
129                 switch (ffado_streaming_get_playback_stream_type(dev,i)) {
130                         case ffado_stream_type_audio:
131                                 if (i<nb_in_channels) {
132                                         /* assign the audiobuffer to the stream */
133                                         ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(audiobuffers_in[i]));
134                                         ffado_streaming_set_playback_buffer_type(dev, i, ffado_buffer_type_float);
135                                         ffado_streaming_playback_stream_onoff(dev, i, 1);
136                                 } else {
137                                         ffado_streaming_set_playback_stream_buffer(dev, i, (char *)nullbuffer);
138                                         ffado_streaming_set_playback_buffer_type(dev, i, ffado_buffer_type_float);
139                                         ffado_streaming_playback_stream_onoff(dev, i, 1);
140                                 }
141                                 break;
142                                 // this is done with read/write routines because the nb of bytes can differ.
143                         case ffado_stream_type_midi:
144                         default:
145                                 break;
146                 }
147         }
148        
149         /* open the files to write to*/
150         FILE* fid_out[nb_out_channels];
151         FILE* fid_in[nb_in_channels];
152         char name[256];
153
154         for (i=0;i<nb_out_channels;i++) {
155                 snprintf(name,sizeof(name),"out_ch_%02d",i);
156
157                 fid_out[i]=fopen(name,"w");
158
159                 ffado_streaming_get_playback_stream_name(dev,i,name,sizeof(name));
160                 fprintf(fid_out[i],"Channel name: %s\n",name);
161                 switch (ffado_streaming_get_playback_stream_type(dev,i)) {
162                 case ffado_stream_type_audio:
163                         fprintf(fid_out[i],"Channel type: audio\n");
164                         break;
165                 case ffado_stream_type_midi:
166                         fprintf(fid_out[i],"Channel type: midi\n");
167                         break;
168                 case ffado_stream_type_unknown:
169                         fprintf(fid_out[i],"Channel type: unknown\n");
170                         break;
171                 default:
172                 case ffado_stream_type_invalid:
173                         fprintf(fid_out[i],"Channel type: invalid\n");
174                         break;
175                 }
176
177         }
178         for (i=0;i<nb_in_channels;i++) {
179                 snprintf(name,sizeof(name),"in_ch_%02d",i);
180                 fid_in[i]=fopen(name,"w");
181
182                 ffado_streaming_get_capture_stream_name(dev,i,name,sizeof(name));
183                 fprintf(fid_in[i], "Channel name: %s\n",name);
184                 switch (ffado_streaming_get_capture_stream_type(dev,i)) {
185                 case ffado_stream_type_audio:
186                         fprintf(fid_in[i], "Channel type: audio\n");
187                         break;
188                 case ffado_stream_type_midi:
189                         fprintf(fid_in[i], "Channel type: midi\n");
190                         break;
191                 case ffado_stream_type_unknown:
192                         fprintf(fid_in[i],"Channel type: unknown\n");
193                         break;
194                 default:
195                 case ffado_stream_type_invalid:
196                         fprintf(fid_in[i],"Channel type: invalid\n");
197                         break;
198                 }
199         }
200
201         // start the streaming layer
202         ffado_streaming_prepare(dev);
203         start_flag = ffado_streaming_start(dev);
204
205         fprintf(stderr,"Entering receive loop (%d,%d)\n",nb_in_channels,nb_out_channels);
206         while(run && start_flag==0) {
207                 retval = ffado_streaming_wait(dev);
208                 if (retval < 0) {
209                         fprintf(stderr,"Xrun\n");
210                         ffado_streaming_reset(dev);
211                         continue;
212                 }
213                
214 //              ffado_streaming_transfer_buffers(dev);
215                 ffado_streaming_transfer_capture_buffers(dev);
216                 ffado_streaming_transfer_playback_buffers(dev);
217                
218                 nb_periods++;
219
220                 if((nb_periods % 32)==0) {
221 //                      fprintf(stderr,"\r%05d periods",nb_periods);
222                 }
223
224                 for(i=0;i<nb_in_channels;i++) {
225                        
226                        
227                         switch (ffado_streaming_get_capture_stream_type(dev,i)) {
228                         case ffado_stream_type_audio:
229                                 // no need to get the buffers manually, we have set the API internal buffers to the audiobuffer[i]'s
230 //                              //samplesread=freebob_streaming_read(dev, i, audiobuffer[i], PERIOD_SIZE);
231                                 samplesread=PERIOD_SIZE;
232                                 break;
233                         case ffado_stream_type_midi:
234                                 samplesread=ffado_streaming_read(dev, i, audiobuffers_out[i], PERIOD_SIZE);
235                                 break;
236                         default: break;
237                         }
238        
239 //                      fprintf(fid_in[i], "---- Period read  (%d samples) ----\n",samplesread);
240 //                      hexDumpToFile(fid_in[i],(unsigned char*)audiobuffers_in[i],samplesread*sizeof(float)+1);
241                 }
242
243                 for(i=0;i<nb_out_channels;i++) {
244                         ffado_sample_t *buff;
245                         int sampleswritten=0;
246                         if (i<nb_in_channels) {
247                                 buff=audiobuffers_out[i];
248                         } else {
249                                 buff=nullbuffer;
250                         }
251                        
252                         switch (ffado_streaming_get_playback_stream_type(dev,i)) {
253                         case ffado_stream_type_audio:
254 //                              sampleswritten=freebob_streaming_write(dev, i, buff, PERIOD_SIZE);
255                                 sampleswritten=PERIOD_SIZE;
256                                 break;
257                         case ffado_stream_type_midi:
258 //                              sampleswritten=freebob_streaming_write(dev, i, buff, PERIOD_SIZE);
259                                 break;
260                         default: break;
261                         }
262 //                      fprintf(fid_out[i], "---- Period write (%d samples) ----\n",sampleswritten);
263 //                      hexDumpToFile(fid_out[i],(unsigned char*)buff,sampleswritten*sizeof(ffado_sample_t));
264                 }
265
266         }
267
268         fprintf(stderr,"\n");
269
270         fprintf(stderr,"Exiting receive loop\n");
271        
272         ffado_streaming_stop(dev);
273
274         ffado_streaming_finish(dev);
275
276         for (i=0;i<nb_out_channels;i++) {
277                 fclose(fid_out[i]);
278
279         }
280         for (i=0;i<nb_in_channels;i++) {
281                 fclose(fid_in[i]);
282         }
283        
284         for (i=0;i<nb_in_channels;i++) {
285                 free(audiobuffers_in[i]);
286                 free(audiobuffers_out[i]);
287         }
288         free(nullbuffer);
289         free(audiobuffers_in);
290         free(audiobuffers_out);
291
292   return EXIT_SUCCESS;
293 }
Note: See TracBrowser for help on using the browser.