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

Revision 786, 10.6 kB (checked in by ppalmers, 16 years ago)

add sine output

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