Changeset 786

Show
Ignore:
Timestamp:
12/29/07 04:02:04 (16 years ago)
Author:
ppalmers
Message:

add sine output

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libffado/tests/streaming/teststreaming3.c

    r783 r786  
    4343#include "debugtools.h" 
    4444 
     45#include <math.h> 
     46 
    4547int run; 
    46  
    47 static void sighandler (int sig) 
    48 { 
    49         run = 0; 
    50         set_realtime_priority(0); 
    51 } 
    5248 
    5349int set_realtime_priority(unsigned int prio) 
     
    8278} 
    8379 
     80static void sighandler (int sig) 
     81{ 
     82    run = 0; 
     83    set_realtime_priority(0); 
     84} 
     85 
    8486int main(int argc, char *argv[]) 
    8587{ 
    8688 
    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); 
     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); 
    325338 
    326339  return EXIT_SUCCESS;