Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PCM tone generator

Name: Anonymous 2011-10-30 12:52

Hey /prog/

I'm trying to teach myself how to generate PCM audio in C, and I can't figure out why this isn't generating a clear sine wave signal when I pipe it to aplay.  Any ideas?


#include <math.h>

#define MIDDLE_C 261.626
#define HZ 8000.0
#define ONE_CYCLE 2.0
#define PI 3.14159265

int main() {

    float frames = HZ / MIDDLE_C;
    float i;
    float n;
    unsigned int x;
    int s, v;

    for(v=0; v<MIDDLE_C; v++) {
        for(i=0; i<frames; i++) {
            n = (1 + sin((i / frames) * (PI * 2))) / 2;
            x = (int)round(0xFF * n);
            putchar(x);
        }
    }

    return 0;

}

Name: >>5 2011-10-30 18:41

>>9
Weird. What did you change to make the audio output work right?

>>18
My original thought (which seemed not to be the case in the end) was that the audio device expected signed chars in the PCM data. So the signed, correct sine wave looks like:

00 ++++++++++++++++ 00 -------------- 00
00 01 ... 7f ... 01 00 ff .. 80 .. ff 00

If however, assuming it's unsigned, you send these bytes:

80 81 ... ff ... 81 80 7f .. 00 .. 7f 80

The wave will start too low, then underflow from 80 to 7f and back.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List