Skip to contentSkip to conversation

Alsa buffer

DISCUSSION arif setiawan 2 participants 2 messages Jul 14, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to get meaningful voice from ALSA buffer. I read tutorial on
http://www.suse.de/~mana/alsa090_howto.html<http://www.suse.de/%7Emana/alsa090_howto.html>and
realize that if alsa use 16 bit format value then I have to byte shift to store it in 8 bit unsigned char buffer. I see this in sine wave generator in tutorial. To confirm it, I try to save the output into file and open it in Matlab. I want to know if this is correct, because when I try saving alsa buffer on a from recording I still get noise not the voice.

Thank you.

Best regards,

Arif

This is my implementation :

int main()

{
    int nchannel = 1;
    int periodsize = 8192;
    int frame_size = nchannel * (16 / 8);
    int num_frames = periodsize / frame_size;

    unsigned char *data;
    int l2;
    short s;

    data = (unsigned char *)malloc(periodsize);

    // fill the buffer
    for(l2 = 0; l2 < num_frames; l2++)
    {
        s1 = (l2 % 128) * 100 - 5000;
        data[2*l2] = (unsigned char)s1;
        data[2*l2+1] = s1 >> 8;
    }

    // directly save
    FILE *fp1 = fopen("sineout1", "w");

    for (int i = 0; i < periodsize; i++)
    {
        fprintf_s(fp1, "%d\n", data[i]);
    }

    fclose(fp1);

    // with bit shifting from char to short
    FILE *fp2 = fopen("sineout2", "w");

    for (int i = 0; i < num_frames; i++)
    {
        short newval = data[2*i+1] << 8;
        newval += data[2*i];

        fprintf_s(fp2, "%d\n", newval);
    }

    fclose(fp2);

    return 0;
}
#1 lore raw
Quoted 7 lines · On Tue, 14 Jul 2009 08:55:58 +0700 arif setiawan <[email protected]>…

into file and open it in Matlab. I want to know if this is correct, because when I try saving alsa buffer on a from recording I still get noise not the voice.

    // directly save
    FILE *fp1 = fopen("sineout1", "w");

FILE *fp1 = fopen("sineout1", "wb");

Quoted 4 lines
    for (int i = 0; i < periodsize; i++)
    {
        fprintf_s(fp1, "%d\n", data[i]);

You are printing formatted text data here, it needs to be binary. fwrite

Quoted 6 lines
    }

    fclose(fp1);

    // with bit shifting from char to short
    FILE *fp2 = fopen("sineout2", "w");

FILE *fp2 = fopen("sineout2", "wb");

Quoted 7 lines
    for (int i = 0; i < num_frames; i++)
    {
        short newval = data[2*i+1] << 8;
        newval += data[2*i];

        fprintf_s(fp2, "%d\n", newval);

You are printing formatted text data here, it needs to be binary. fwrite

Quoted 11 lines
    }

    fclose(fp2);

    return 0;
}

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel