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

Revision 783, 8.6 kB (checked in by ppalmers, 15 years ago)

cleanup time/wait/sleep code

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