Pointers to Function Members (2)

waterfall

This waterfall display looks weird, but it is because the sound signals from two devices, IC-7410 and Soft66LC4, are interleaved almost line by line. A narrow band signal is from IC-7410, and a broad band from Soft66LC4.

I need to change the whole structure of the program to have two waterfall windows instead of interleaved lines, but it seems the following code works as far as the callback function for alsa audio system is concerned.

Sound *mysound1;
Sound *mysound2;

int main(int argc, char *argv[])
{
    if (argc == 5) {
    	mysound1 = new Sound{argv[2], argv[3], argv[4]};
    } else if (argc == 8) {
        mysound1 = new Sound{argv[2], argv[3], argv[4]};
        mysound2 = new Sound{argv[5], argv[6], argv[7]};
    } else {
	cout << "Usage (IC-7410 only)      : " << argv[0] <<
	    " /dev/ttyUSB0 hw:2,0 32000 1 \n";
	cout << "Usage (IC-7410 and Soft66): " << argv[0] <<
	    " /dev/ttyUSB0 hw:2,0 32000 1 hw:0,0 48000 2 \n";
	return false;
    }
class Sound {
  public:
    Sound(char *, const char *, const char *);
    int asound_set_hwparams(snd_pcm_t * handle, snd_pcm_hw_params_t * hwparams);
    int asound_set_swparams(snd_pcm_t * handle, snd_pcm_sw_params_t * swparams);
    void asound_async_callback(snd_async_handler_t * ahandler);

    snd_pcm_sframes_t buffer_size;
    snd_pcm_sframes_t period_size;
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *hwparams;
    snd_pcm_sw_params_t *swparams;
    snd_async_handler_t *ahandler;
extern Sound *mysound1;
extern Sound *mysound2;

void asound_callback1_wrapper(snd_async_handler_t * ahandler)
{
    mysound1->asound_async_callback(ahandler);
}

void asound_callback2_wrapper(snd_async_handler_t * ahandler)
{
    mysound2->asound_async_callback(ahandler);
}
Sound::Sound(char *s, const char *r, const char *c)
{
    sound_device = s;
    rate = atoi(r);
    channels = atoi(c);

    snd_pcm_hw_params_alloca(&hwparams);
    snd_pcm_sw_params_alloca(&swparams);

    if (0) {
    } else if (channels == 1) {
    	err = snd_async_add_pcm_handler(&ahandler, handle, asound_callback1_wrapper, samples);	/* callback */
    } else if (channels == 2) {
    	err = snd_async_add_pcm_handler(&ahandler, handle, asound_callback2_wrapper, samples);	/* callback */
    }

https://github.com/jh1ood/sprigmm/tree/develop

Pointers to Function Members

How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?

I did not know it is an FAQ, and wasted many hours trying this way and that.

The_C++_Programming_Language,_Fourth_Edition

In the book, the topic appears around here:

pointers

    if (argc == 5) {
        Sound ic7410 {argv[2], argv[3], argv[4]};
    } else if (argc == 8) {
        Sound ic7410 {argv[2], argv[3], argv[4]};
        Sound soft66 {argv[5], argv[6], argv[7]};
    } else {
    	cout << "Usage (IC-7410 only)      : " << argv[0] << " /dev/ttyUSB0 hw:2,0 32000 1 \n";
    	cout << "Usage (IC-7410 and Soft66): " << argv[0] << " /dev/ttyUSB0 hw:2,0 32000 1 hw:0,0 48000 2 \n";
    	return false;
    }

We have two audio devices, ic7410 and soft66, and each requires its callback function.

class Sound {
public:
	Sound(char*, const char*, const char*);
//	void asound_async_callback(snd_async_handler_t * ahandler); /* fix it */
private:
	int  asound_set_hwparams(snd_pcm_t * handle, snd_pcm_hw_params_t * hwparams);
	int  asound_set_swparams(snd_pcm_t * handle, snd_pcm_sw_params_t * swparams);
	snd_pcm_hw_params_t *hwparams;
	snd_pcm_sw_params_t *swparams;
}