简体   繁体   中英

Raspberry Pi generate tone with PiFM

While back I came across PiFM and decided to learn a bit about audio and modulation. I am trying to write an AFSK modulator, but first I wanted to generate pure tones, like 1000Hz. I am using code from the PiFM project ( https://github.com/rm-hull/pifm/blob/master/pifm.cpp ) which reads in a WAV file and shoots it out over RF. I want to do the same but I want to shoot out pure tones over RF.

Here is one of my attempts:

void playSineWave(float frequency, int duration, float samplerate)
{
SampleSink* ss;

ss =  new Outputter(samplerate);

int bufferLen = duration * samplerate;
float* buffer = new float[bufferLen];

for (int i = 0; i < bufferLen; i++) {
    float amplitude = 6000;

    buffer[i] = amplitude * sin( (2.f * float(M_PI) * i * frequency) / samplerate );
}

cout << "Buffer length: " << bufferLen << endl;

ss->consume(buffer, bufferLen);

delete [] buffer;
}

I would use it as such playSineWave(1000, 5, 22050); to play 1000Hz tone for 5 seconds. But I get either nothing or noise. Can you guys suggest how to fix it or perhaps some good reading material?

Edit: Changed code to fix issue with amplitude. Still no tone.

I'm not sure if it's intentional to make amplitude change over time. Anyhow, i / bufferLen * 32760 is evaluated as (i / bufferLen) * 32760 and i / bufferLen is going to be 0 for all i smaller than bufferLen (look up integer division in C/C++), which is the case because of for (int i = 0; i < bufferLen; i++) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM