mahaju
Posts: 28
Joined: Fri Mar 26, 2021 1:25 am

Generating puretone sinewave in pi zero using C++

Tue Oct 12, 2021 10:55 am

I am trying to figure out how to generate 1 kHz tone in raspberry pi zero W board using ALSA api and C++, but I don't seem to be getting anywhere. Strangely there seems to be no beginner friendly step-by-step tutorial on generating sound using ALSA, when I thought it was the most comomn way of generating sound in linux.

I am using a pi zero W board with raspberry pi OS. I have a USB sound card connected to a USB hub, which is connected to the USB port on the pi zero board. I can play wav files from both VLC and command line using aplay, so there is no problem with the hardware. I can also run the small test program given here: viewtopic.php?t=15075, so I don't think there is any problem with my ALSA installation either.

Attempt 1:
I have tried running the test/pcm.c sample code given in ALSA's website: https://www.alsa-project.org/alsa-doc/a ... ample.html
I compile this as

Code: Select all

gcc main.cpp -o Main -lasound -Wall -fpermissive
I get a couple of warnings and then an error saying no match for operator++ for snd_pcm_format_t (which is an enum type). the error is specifically at the line

Code: Select all

for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {
(under case 'o' in the part that parses the command line arguments). I am stuck after this and not sure what to do.

Attempt 2:
I am trying out the program given here: http://equalarea.com/paul/alsa-audio.html, under "A Minimal Interrupt-Driven Program". This program compiles after some modifications, and I am manually calculating 1kHz sinewave samples at 48000 Hz sampling frequency. Problem is the generated tone doesn't sound anything like 1 kHz pure tone. I have a hunch this distortion is because the output is saturated, since it is quite loud, but I need some confirmation that this is the case. I have tried to simply reduce the amplitude of the sine function by multiplying it with 0.1, but that still generates the same distorted tone sound, only a lot smaller. I can't find the proper way to reduce the volume in ALSA api either. This is the code I am testing:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <poll.h>
#include <math.h>
#include <alsa/asoundlib.h>
      
snd_pcm_t *playback_handle;
// short buf[4096];
short buf[48*2];        // try 1000 Hz @48000, 48 samples @48000 = 1 ms

void sine_gen(){
    // call once to generate the sine values
    // if this works do this inside callback function later on
    double tempSin = 0.0;
    #if 1
    // check if buf[] contains 2 channels interleaved
    for(int i = 0; i<48*2; i+=2){
        tempSin = sin(2.0*M_PI*1000*i/48000);
        buf[i] = (short)(tempSin * pow(2.0, 15) );
        buf[i+1] = buf[i];
    }
    #else
    // check if buf[] contains single channel data
    for(int i = 0; i<48*2; i++){
        tempSin = sin(2*M_PI*1000*i/48000);
        buf[i] = (short)(tempSin * pow(2.0, 15) );
    }   
    #endif
    
}

void SetAlsaMasterVolume(long volume)
{
    long min, max;
    snd_mixer_t *handle;
    snd_mixer_selem_id_t *sid;
    const char *card = "default";
    const char *selem_name = "Master";

    snd_mixer_open(&handle, 0);
    snd_mixer_attach(handle, card);
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);

    snd_mixer_selem_id_alloca(&sid);
    snd_mixer_selem_id_set_index(sid, 0);
    snd_mixer_selem_id_set_name(sid, selem_name);
    snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);

    snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
    snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);

    snd_mixer_close(handle);
}

int
playback_callback (snd_pcm_sframes_t nframes)
{
    int err;

    // printf ("playback callback called with %u frames\n", nframes);

    /* ... fill buf with data ... */
    // if this part is empty, buf[] should have been filled correctly inside sine_gen()
    
    if ((err = snd_pcm_writei (playback_handle, buf, nframes)) < 0) {
        fprintf (stderr, "write failed (%s)\n", snd_strerror (err));
    }
    
    return err;
}
      
int main (int argc, char *argv[])
{

    snd_pcm_hw_params_t *hw_params;
    snd_pcm_sw_params_t *sw_params;
    snd_pcm_sframes_t frames_to_deliver;
    int nfds;
    int err;
    struct pollfd *pfds;
    
    sine_gen();     // call this once to generate sinewave

    if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        fprintf (stderr, "cannot open audio device %s (%s)\n", 
             argv[1],
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
        fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
        fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
        fprintf (stderr, "cannot set access type (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
        fprintf (stderr, "cannot set sample format (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    unsigned int f_s = 48000;
    // if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, 44100, 0)) < 0) {        // causes segmentation fault; see // https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___h_w___params.html#ga6014e0e1ec7934f8c745290e83e59199
    if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &f_s, 0)) < 0) {
        fprintf (stderr, "cannot set sample rate (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
        fprintf (stderr, "cannot set channel count (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
        fprintf (stderr, "cannot set parameters (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    snd_pcm_hw_params_free (hw_params);
    
    /* tell ALSA to wake us up whenever 4096 or more frames
       of playback data can be delivered. Also, tell
       ALSA that we'll start the device ourselves.
    */

    if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
        fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    if ((err = snd_pcm_sw_params_current (playback_handle, sw_params)) < 0) {
        fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    // if ((err = snd_pcm_sw_params_set_avail_min (playback_handle, sw_params, 4096)) < 0) {        // change this as per the size of the buffer used
    if ((err = snd_pcm_sw_params_set_avail_min (playback_handle, sw_params, 48*2)) < 0) {       
        fprintf (stderr, "cannot set minimum available count (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    if ((err = snd_pcm_sw_params_set_start_threshold (playback_handle, sw_params, 0U)) < 0) {
        fprintf (stderr, "cannot set start mode (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    if ((err = snd_pcm_sw_params (playback_handle, sw_params)) < 0) {
        fprintf (stderr, "cannot set software parameters (%s)\n",
             snd_strerror (err));
        exit (1);
    }

    /* the interface will interrupt the kernel every 4096 frames, and ALSA
       will wake up this program very soon after that.
    */

    if ((err = snd_pcm_prepare (playback_handle)) < 0) {
        fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    
    // SetAlsaMasterVolume(0);      // attempt changing volume      // didn't make any difference

    while (1) {

        /* wait till the interface is ready for data, or 1 second
           has elapsed.
        */

        if ((err = snd_pcm_wait (playback_handle, 1000)) < 0) {
                fprintf (stderr, "poll failed (%s)\n", strerror (errno));
                break;
        }              

        /* find out how much space is available for playback data */

        if ((frames_to_deliver = snd_pcm_avail_update (playback_handle)) < 0) {
            if (frames_to_deliver == -EPIPE) {
                fprintf (stderr, "an xrun occured\n");
                break;
            } else {
                fprintf (stderr, "unknown ALSA avail update return value (%d)\n", 
                     frames_to_deliver);
                break;
            }
        }

        // frames_to_deliver = frames_to_deliver > 4096 ? 4096 : frames_to_deliver;
        frames_to_deliver = frames_to_deliver > (48*2) ? (48*2) : frames_to_deliver;

        /* deliver the data */

        if (playback_callback (frames_to_deliver) != frames_to_deliver) {
                fprintf (stderr, "playback callback failed\n");
            break;
        }
    }

    snd_pcm_close (playback_handle);
    exit (0);
    
    return 0;
}  
Can someone please point to me what I am doing wrong in attempts 1 and 2 above?

danjperron
Posts: 3871
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Generating puretone sinewave in pi zero using C++

Tue Oct 12, 2021 6:04 pm

I found a bug when you generate the sinewave.

pow(2,15) is 32768. The sine of PI/4 is 1.0 then 32768. but 32768 is bigger than short.

The best thing is to set the value to 32767! (pow(2,15)-1.0)
why not just a define
#define MAXPEAK 32767 => MAXPEAK * sin(.....

This way you make the negative and positive side symetric!

This is the loop data in your sinus generator. Check the index 12.
0:0
2:8480
4:16383
6:23170
8:28377
10:31651
12:-32768
14:31651
16:28377
18:23170
20:16384
22:8480
24:0
26:-8480
28:-16383
30:-23170
32:-28377
34:-31651
36:-32768
38:-31651
40:-28377
42:-23170
44:-16384
46:-8480
48:0

dsyleixa123
Posts: 1556
Joined: Mon Jun 11, 2018 11:22 am

Re: Generating puretone sinewave in pi zero using C++

Thu Oct 14, 2021 6:48 pm

#define MAXPEAK 32767
IIRC, it's already defined in stdint.h
INT16_MAX

Return to “C/C++”