问题描述
如何在C/C ++中生成特定频率的声音.我运行Ubuntu 10.04并使用gcc. Windows的TurboC上有一个void sound(int frequency)
函数. gcc是否有等效功能?
How can I generate sound of a particular frequency in C/C++ . I run Ubuntu 10.04 and use gcc. There is a void sound(int frequency)
function on TurboC for Windows. Is there an equivalent for gcc?
推荐答案
以下是利用PortAudio库生成给定频率的方波的代码.在Linux上,使用gcc buzzer.c -o buzzer -lportaudio
进行编译.对于Windows也应该编译良好.我不知道sound(int frequency)
的行为如何,但是下面应该可以模拟老式蜂鸣器的用法.您可能需要一个portaudio-devel
(或与Ubuntu,portaudio-dev等效)?对于Pulse Audio,您的存储库中可能需要一些较新的PortAudio版本.编译不是问题.您可以按照 WTFPL 许可的条款使用以下代码. :-)(它来自PortAudio示例)
Below is a code utilizing PortAudio library to generate a square audio wave of given frequency.On Linux compile with gcc buzzer.c -o buzzer -lportaudio
. Should compile fine for Windows as well. I do not know how the sound(int frequency)
behaves exactly, but below should be able to simulate any usage of old-style buzzers. You might need a portaudio-devel
(or equivalent for Ubuntu, portaudio-dev?) package and for Pulse Audio maybe some newer version of PortAudio that is in your repo. Compiling it is not a problem. You can use the below code on the terms of WTFPL license. :-) (it is derived from a PortAudio example)
#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生成特定频率的声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!