PDA

View Full Version : sine wave not generated with click of pushButton



duma
9th August 2011, 19:14
Hi guys,
So I have a program which generates a sine wave. I have compiled and run it in Linux terminal successfully. Now, I wish to integrate it in Qt to build a gui which takes in values of frequency and amplitude and generates the sine wave according to those specifications. I have designed the gui in Qt Creator using QMainWindow. My class is called "wave". I have successfully compiled and run the program with no errors. The QMainWindow pops up when I run it. But when I input values for frequency and amplitude and push the pushButton, no sine wave is generated. I'm not sure why.
Any help would be greatly appreciated.
Below is the code under my pushButton function in wave.cpp and the code of any functions that have been called within it:
wave.cpp:

#include <QtGui>
#include <QApplication>
#include <qlineedit.h>
#include "ui_wave.h"
#include <QString>
#include <QObject>
#include <QDebug>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <cstring>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>
#include <sstream>
#include <string>
#include <math.h>
#include <cmath>
#include <iostream>
#include "generate_sine.h"
#include "xrun_recovery.h"
#include "wave.h"
#include "write_loop.h"
using namespace std;


static const char *device = "plughw:0,0"; // playback device
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; // sample format- change to 24-bit
static unsigned int rate = 96000; // stream rate
static unsigned int channels = 128; // count of channels
static unsigned int buffer_time = 500000; // ring buffer length in us
static unsigned int period_time = 100000; // period time in us
static double freq; // sinusoidal wave frequency in Hz
static int verbose = 0; // verbose flag
static int resample = 1; // enable alsa-lib resampling
static int period_event = 0; // produce poll event after each period
static snd_pcm_sframes_t buffer_size;
static snd_pcm_sframes_t period_size;
static snd_output_t *output = NULL;
static double amplitude;
static bool isTrue;


wave::wave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);


}
wave::~wave()
{
delete ui;

}


void wave::on_pushButton_clicked()
{
snd_pcm_t *handle;
snd_pcm_channel_area_t *areas;
signed short *samples;

write_loop(handle, samples, areas);

}



void wave::on_pushButton_2_clicked()
{
isTrue = false;

}


write_loop():

#include <QtGui>
#include <QApplication>
#include <qlineedit.h>
#include "ui_wave.h"
#include <QString>
#include <QObject>
#include <QDebug>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <cstring>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>
#include <sstream>
#include <string>
#include <math.h>
#include <cmath>
#include <iostream>
#include "write_loop.h"
#include "wave.h"
#include "generate_sine.h"
#include "xrun_recovery.h"
using namespace std;

static char *device = "plughw:0,0"; /* playback device */
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
static unsigned int rate = 96000; /* stream rate */
static unsigned int channels = 128; /* count of channels */
static unsigned int buffer_time = 500000; /* ring buffer length in us */
static unsigned int period_time = 100000; /* period time in us */
static double freq = 440; /* sinusoidal wave frequency in Hz */
static int verbose = 0; /* verbose flag */
static int resample = 1; /* enable alsa-lib resampling */
static int period_event = 0; /* produce poll event after each period */
static snd_pcm_sframes_t buffer_size;
static snd_pcm_sframes_t period_size;
static snd_output_t *output = NULL;
static float amplitude;
static bool isTrue;

int write_loop(snd_pcm_t *handle,
signed short *samples,
snd_pcm_channel_area_t *areas)
{
double phase = 0;
signed short *ptr;
int err, cptr;

while (isTrue) {

generate_sine(areas, 0, period_size, &phase);
ptr = samples;
cptr = period_size;
while (cptr > 0) {
err = snd_pcm_writei(handle, ptr, cptr);

if (err == -EAGAIN)
continue;
if (err < 0) {
if (xrun_recovery(handle, err) < 0) {
printf("Write error: %s ", snd_strerror(err));
exit(EXIT_FAILURE);
}
break; /* skip one period */
}
ptr += err * channels;
cptr -= err;
}
}
}

Santosh Reddy
10th August 2011, 05:40
The QMainWindow pops up when I run it. But when I input values for frequency and amplitude and push the pushButton, no sine wave is generated. I'm not sure why.
Any help would be greatly appreciated.
To start with, please help others to understand what is going wrong, set break points in pushbutton slot and check where the control goes, this the one of the first things one would do, please do this, if not already done, and post the results, if you are not able figure out what is going wrong, then the someone can help.

Ginsengelf
10th August 2011, 10:31
Hi, after a quick look it seems like you never set isTrue to true, so probably your while(isTrue) loop will never be executed.

Ginsengelf

duma
10th August 2011, 18:52
Hi, after a quick look it seems like you never set isTrue to true, so probably your while(isTrue) loop will never be executed.

Ginsengelf

Hi when i put isTrue = true, my program terminates when I click the push button. I'm not sure why this is.

Okay guys, so I don't seem to have explained things very clearly, so I'm going to start a new thread with a clear and long explanation as I have two problems now.