Changeset 1191

Show
Ignore:
Timestamp:
08/04/08 06:29:03 (4 months ago)
Author:
cdavid
Message:

Improve simple example with pcm drain.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/audiolab/scikits/audiolab/soundio/simple2.c

    r556 r1191  
    33 
    44#include <alsa/asoundlib.h> 
     5 
     6int play(snd_pcm_t *pcm) 
     7{ 
     8        int16_t buff[1024 * 16]; 
     9        int i, j; 
     10        snd_pcm_uframes_t frames; 
     11 
     12        for (i = 0; i < 4; ++i) { 
     13            for (j = 0; j < 1024 * 16; ++j) { 
     14                buff[j] = random() & 0xff; 
     15            } 
     16            frames = snd_pcm_writei(pcm, buff, 1024 * 16);  
     17            fprintf(stderr, "%lu\n", frames); 
     18        } 
     19 
     20        return 0; 
     21} 
     22 
     23int set(snd_pcm_t *pcm, snd_pcm_format_t format, snd_pcm_access_t access,  
     24        unsigned int channels, unsigned int samplerate) 
     25{ 
     26        int st; 
     27 
     28        st = snd_pcm_set_params(pcm, format, access, channels, samplerate, 
     29                                1, 1000000);       
     30        if (st < 0) { 
     31                printf("Output failed: %s\n", snd_strerror(st)); 
     32                return st; 
     33        } 
     34 
     35        return 0; 
     36} 
    537 
    638int main() 
     
    1446        snd_output_t *output; 
    1547 
    16         uint8_t buff[1024 * 16]; 
    1748        int i, j; 
    1849        snd_pcm_sframes_t frames; 
     
    3061        } 
    3162 
    32         st = snd_pcm_set_params(pcm,  
    33                                 SND_PCM_FORMAT_S16,  
    34                                 SND_PCM_ACCESS_RW_INTERLEAVED,  
    35                                 nc,  
    36                                 rrate, 
    37                                 1, 
    38                                 1000000);       
    39         if (st < 0) { 
    40                 printf("Output failed: %s\n", snd_strerror(st)); 
    41                 return -1; 
    42         } 
    43         snd_pcm_dump(pcm, output); 
     63        set(pcm, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 44100); 
    4464 
    45         fprintf(stderr, "SND_PCM_FORMAT_U8 : %d\n", SND_PCM_FORMAT_U8); 
    46         fprintf(stderr, "SND_PCM_FORMAT_S16 : %d\n", SND_PCM_FORMAT_S16); 
    47         fprintf(stderr, "SND_PCM_FORMAT_FLOAT : %d\n", SND_PCM_FORMAT_FLOAT); 
    48         fprintf(stderr, "SND_PCM_ACCESS_RW_INTERLEAVED : %d\n", SND_PCM_ACCESS_RW_INTERLEAVED); 
    49         for (i = 0; i < 16; ++i) { 
    50             for (j = 0; j < 1024 * 16; ++j) { 
    51                 buff[j] = random() & 0xff; 
    52             } 
    53             frames = snd_pcm_writei(pcm, buff, 1024 * 16);  
    54             fprintf(stderr, "%u\n", frames); 
    55         } 
     65        play(pcm); 
     66        snd_pcm_drain(pcm); 
     67 
     68        set(pcm, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 8000); 
     69 
     70        play(pcm); 
     71 
     72        //snd_pcm_dump(pcm, output); 
     73 
    5674        snd_output_close(output); 
    5775        snd_pcm_close(pcm);