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

Revision 194, 7.8 kB (checked in by pieterpalmers, 18 years ago)

- code cleanup in the streaming part.
- calculate ISO connection parameters instead of user-supplying them
- SSE code improvement

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