2009-11-16 45 views
1

當我編譯ALSA的pcm_min.c例如與來自ALSA的pcm_min.c示例的警告/錯誤。可能的問題?

gcc -Wall -lasound pcm_min.c -o pcm_min 

一切都很好,但運行它,我得到預期的白噪聲,但我也得到這樣的警告/錯誤:

Short write (expected 16384, wrote 7616) 

哪個來從最後一條if語句開始。

#include <alsa/asoundlib.h> 

static char *device = "default";      /* playback device */ 

snd_output_t *output = NULL; 
unsigned char buffer[16*1024];       /* some random data */ 

int main(void) 
{ 
     int err; 
     unsigned int i; 
     snd_pcm_t *handle; 
     snd_pcm_sframes_t frames; 

     for (i = 0; i < sizeof(buffer); i++) 
       buffer[i] = random() & 0xff; 

     if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { 
       printf("Playback open error: %s\n", snd_strerror(err)); 
       exit(EXIT_FAILURE); 
     } 
     if ((err = snd_pcm_set_params(handle, 
             SND_PCM_FORMAT_U8, 
             SND_PCM_ACCESS_RW_INTERLEAVED, 
             1, 
             48000, 
             1, 
             500000)) < 0) { /* 0.5sec */ 
       printf("Playback open error: %s\n", snd_strerror(err)); 
       exit(EXIT_FAILURE); 
     } 

     for (i = 0; i < 16; i++) { 
       frames = snd_pcm_writei(handle, buffer, sizeof(buffer)); 
       if (frames < 0) 
         frames = snd_pcm_recover(handle, frames, 0); 
       if (frames < 0) { 
         printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); 
         break; 
       } 
       if (frames > 0 && frames < (long)sizeof(buffer)) 
         printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); 
     } 

     snd_pcm_close(handle); 
     return 0; 
} 

有人可以看到爲什麼會發生此警告/錯誤嗎?

擁抱, 路易絲

回答

3

時,有兩種接收到的信號或欠載的snd_pcm_writei()功能可能會返回小於sizeof(buffer)。在你的情況,似乎你在混合字節和幀。調用的最後一個參數是緩衝區中的幀數。由於您將緩衝區中的字節數傳遞給您,因此您會看到欠載。

+0

我不明白你想讓我試試。你能解釋一下嗎? – Louise 2009-11-16 06:21:24

+0

我編輯了我的答案,因爲你傳遞的是字節數而不是幀數。 – Gonzalo 2009-11-16 06:29:10

+0

非常酷。現在我自己的實現不會崩潰=)但我仍然得到相同的警告/錯誤。順便說一句。如果我刪除for循環(當然還保留它的內容),怎麼會這樣,我聽不到任何聲音? – Louise 2009-11-16 06:48:34

1

我也有這個例子的一些問題。我修改了一下,現在它工作。

#include <stdio.h> 
#include <stdlib.h> 
#include <alsa/asoundlib.h> 

static char *device = "default"; /* playback device */ 
snd_output_t *output = NULL; 
unsigned char buffer[16*1024]; /* some random data */ 

int main(void) 
{ 
    int err; 
    unsigned int i; 
    snd_pcm_t *handle; 
    snd_pcm_sframes_t frames; 
    snd_pcm_uframes_t bufferSize, periodSize; 

    for (i = 0; i < sizeof(buffer); i++) 
     buffer[i] = random() & 0xff; 

    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { 
     printf("Playback open error: %s\n", snd_strerror(err)); 
     exit(EXIT_FAILURE); 
    } 
    if ((err = snd_pcm_set_params(handle, 
            SND_PCM_FORMAT_S16_LE, 
            SND_PCM_ACCESS_RW_INTERLEAVED, 
            1, //channels 
            44100, //sample rate 
            1, //allow resampling 
            500000) //required latency in us 
     ) < 0) { 
     printf("Playback open error: %s\n", snd_strerror(err)); 
     exit(EXIT_FAILURE); 
    } 

    if ((err = snd_pcm_prepare(handle)) < 0) { 
     printf("Pcm prepare error: %s\n", snd_strerror(err)); 
     exit(EXIT_FAILURE); 
    } 

    if ((err = snd_pcm_get_params(handle, &bufferSize, &periodSize)) < 0) { 
     printf("Pcm get params error: %s\n", snd_strerror(err)); 
     exit(EXIT_FAILURE); 
    } 
    printf("Buffer size:%d, Period size:%d\n", (int)bufferSize, (int)periodSize); 

    for (i = 0; i < 16; i++) { 
     frames = snd_pcm_writei(handle, buffer, periodSize); 
     if (frames < 0) 
      frames = snd_pcm_recover(handle, frames, 0); 
     if (frames < 0) { 
      printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); 
      break; 
     } 
     if (frames > 0 && frames < (long)periodSize) 
      printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); 
    } 
    snd_pcm_close(handle); 
    return 0; 
} 
相關問題