4

我试图让 ac 程序使用 ALSA 向我的树莓派的音频输出发送一个正弦波音调,我正在生成正弦波值的缓冲区,但是当我发送它们时,它听起来不像正弦波——而是像中低频噪声?我已经将数组中的值转储到 .csv 中并在 excel 中绘制以验证正弦波是否良好只是想知道在这个程序中有一个根本不正确的东西,如果有人能发现一些东西,我将非常感激,谢谢

编辑:下面的最终工作代码!!!

#include <alsa/asoundlib.h>
#include <alsa/pcm.h>
#include <math.h>
#define BUFFER_LEN 48000

static char *device = "default";                       //soundcard
snd_output_t *output = NULL;
float buffer [BUFFER_LEN];


int main(void)
{
    int err;
    int j,k;

    int f = 440;                //frequency
    int fs = 48000;             //sampling frequency

    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;


    // ERROR HANDLING

    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_FLOAT,
                                  SND_PCM_ACCESS_RW_INTERLEAVED,
                                  1,
                                  48000,
                                  1,
                                  500000)) < 0) {   
            printf("Playback open error: %s\n", snd_strerror(err));
            exit(EXIT_FAILURE);


    }

    // SINE WAVE
    printf("Sine tone at %dHz ",f);

        for (k=0; k<BUFFER_LEN; k++){

            buffer[k] = (sin(2*M_PI*f/fs*k));                 //sine wave value generation                        
            }       

        for (j=0; j<5; j++){
            frames = snd_pcm_writei(handle, buffer, BUFFER_LEN);    //sending values to sound driver
            }

    snd_pcm_close(handle);
    return 0;

}
4

1 回答 1

4

您已经配置了样本格式SND_PCM_FORMAT_U8,但实际缓冲区包含 32 位浮点样本。

使用SND_PCM_FORMAT_FLOAT或将缓冲区定义为unsigned char.

此外,您混淆了初始化buffer循环和播放数据的循环以及许多字节/帧数,fs这是错误的;你需要使用这样的东西:

for (i = 0; i < BUFFER_LEN; i++)
    buffer [i] = sin(2*M_PI*f/48000*i);  // sine wave value generation

for (i = 0; i < 10 * 48000 / BUFFER_LEN; i++) { // 10 seconds
    frames = snd_pcm_writei(handle, buffer, BUFFER_LEN);

    if (frames < 0)
        frames = snd_pcm_recover(handle, frames, 0);
    if (frames < 0) {
        printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
        break;
    }
    if (frames > 0 && frames < BUFFER_LEN)
        printf("Short write (expected %li, wrote %li)\n", BUFFER_LEN, frames);
}
于 2013-06-20T16:26:07.013 回答