在 ubuntu 中使用 gcc 生成特定频率的声音?

Posted

技术标签:

【中文标题】在 ubuntu 中使用 gcc 生成特定频率的声音?【英文标题】:Generating sound of a particular frequency using gcc in ubuntu? 【发布时间】:2011-10-06 17:56:43 【问题描述】:

如何在 C/C++ 中生成特定频率的声音。我运行 Ubuntu 10.04 并使用 gcc。 TurboC for Windows 上有一个void sound(int frequency) 函数。是否有 gcc 的等价物?

【问题讨论】:

sox 应该是一个要查找的词,因为它是在 Linux 上生成声音的一种方式。来自***:“在类 Unix 系统上,SoX(作为播放命令)通常作为系统音频文件播放器提供。” 投票关闭作为工具rec,因为这种东西远远超出了当前的ISO C/C++标准范围,所以归结为“可以使用哪个库”。 SDL 版本:***.com/questions/10110905/… 【参考方案1】:

以下是利用 PortAudio 库生成给定频率的方波音频的代码。 在 Linux 上使用 gcc buzzer.c -o buzzer -lportaudio 编译。也应该为 Windows 编译良好。我不知道sound(int frequency) 的行为如何,但下面应该能够模拟老式蜂鸣器的任何使用。您可能需要一个 portaudio-devel(或 Ubuntu 的等价物,portaudio-dev?)包,而对于 Pulse Audio,可能需要一些更新版本的 PortAudio,这些都在您的存储库中。编译它不是问题。您可以根据WTFPL 许可条款使用以下代码。 :-)(它源自一个 PortAudio 示例)

#include <stdio.h>
#include <math.h>
#include "portaudio.h"
#include <stdint.h>
#include <unistd.h> // for usleep()

#define SAMPLE_RATE   (44100)
#define FRAMES_PER_BUFFER  (64)

typedef struct

    uint32_t total_count;
    uint32_t up_count;

    uint32_t counter;
    uint32_t prev_freq;
    uint32_t freq;
 paTestData;

//volatile int freq = 0;

/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int patestCallback( const void *inputBuffer, void *outputBuffer,
                           unsigned long framesPerBuffer,
                           const PaStreamCallbackTimeInfo* timeInfo,
                           PaStreamCallbackFlags statusFlags,
                           void *userData )

    paTestData *data = (paTestData*)userData;
    uint8_t *out = (uint8_t*)outputBuffer;
    unsigned long i;
    uint32_t freq = data->freq;

    (void) timeInfo; /* Prevent unused variable warnings. */
    (void) statusFlags;
    (void) inputBuffer;

    for( i=0; i<framesPerBuffer; i++ )
    
        if(data->up_count > 0 && data->total_count == data->up_count) 
            *out++ = 0x00;
            continue;
        
        data->total_count++;

        if(freq != data->prev_freq) 
            data->counter = 0;
        

        if(freq) 
            int overflow_max = SAMPLE_RATE / freq;
            uint32_t data_cnt = data->counter % overflow_max;
            if(data_cnt > overflow_max/2)
                *out++ = 0xff;
            else 
                *out++ = 0x00;
            
            data->counter++;
        
        else 
            data->counter = 0;
            *out++ = 0;
        
        data->prev_freq = freq;
    

    return paContinue;


static PaStream *stream;
static paTestData data;


void buzzer_set_freq(int frequency)

    data.up_count = 0; // do not stop!
    data.freq = frequency;


void buzzer_beep(int frequency, int msecs)

    data.total_count = 0;
    data.up_count = SAMPLE_RATE * msecs / 1000;
    data.freq = frequency;


int buzzer_start(void)

    PaStreamParameters outputParameters;

    PaError err;
    int i;

    err = Pa_Initialize();
    if( err != paNoError ) goto error;

    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = 1;       /* stereo output */
    outputParameters.sampleFormat = paUInt8; /* 32 bit floating point output */
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;

    err = Pa_OpenStream(
        &stream,
        NULL, /* no input */
        &outputParameters,
        SAMPLE_RATE,
        FRAMES_PER_BUFFER,
        paClipOff,      /* we won't output out of range samples so don't bother clipping them */
        patestCallback,
        &data );
    if( err != paNoError ) goto error;

    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;

    return err;
error:
    Pa_Terminate();
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return err;



int buzzer_stop() 

    PaError err = 0;
    err = Pa_StopStream( stream );
    if( err != paNoError ) goto error;

    err = Pa_CloseStream( stream );
    if( err != paNoError ) goto error;

    Pa_Terminate();

    return err;
error:
    Pa_Terminate();
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return err;

void msleep(int d)
    usleep(d*1000);

int main(void)


    // notes frequency chart: http://www.phy.mtu.edu/~suits/notefreqs.html

    buzzer_start();
    buzzer_set_freq(261);
    msleep(250);
    buzzer_set_freq(293);
    msleep(250);
    buzzer_set_freq(329);
    msleep(250);
    buzzer_set_freq(349);
    msleep(250);
    buzzer_set_freq(392);
    msleep(250);
    buzzer_set_freq(440);
    msleep(250);
    buzzer_set_freq(494);
    msleep(250);
    buzzer_beep(523, 200);
    msleep(250);

    buzzer_stop();

    return 0;

【讨论】:

在许可证上大声笑。这是我第一次看到它。顺便说一句,很好的例子(有点长) 它可以使用多长时间并立即工作 - 无需修复 我一直在寻找这样的东西。谢谢!

以上是关于在 ubuntu 中使用 gcc 生成特定频率的声音?的主要内容,如果未能解决你的问题,请参考以下文章

在ubuntu系统中如何用GCC编译运行一个C程序?

交叉编译用于生成aarch64指令的GCC (9.2)

GCC -msse2 不生成 SIMD 代码

如何产生特定的声音频率?

ARM Cortex M3上的GCC:从特定地址调用函数

gcc 禁用特定文件/文件夹的 -Wall 标志