duma
5th August 2011, 20:19
Hi guys,
I have created a program with QMainWindow on Qt Creator called wave and I have a function called generate_sine() in my main.cpp which I wish to call in my wave.cpp under my pushButton function. How do I do this?
What I tried was: I defined the function in main.cpp, I declared it in the header file(wave.h), and I tried to call/implement it in my wave file(wave.cpp) under the function: on_pushButton_clicked(). This is the way to do it in c++ programming, but its not working. Any help would be greatly appreciated.
Code is below for viewing:
main.cpp:
defining function:
...(some code)...
static int generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase)
{
static double max_phase = 2. * M_PI;
double phase = *_phase;
double step = max_phase*freq/(double)rate;
unsigned char *samples[channels];
int steps[channels];
unsigned int chn;
int format_bits = snd_pcm_format_width(format);
unsigned int maxval = (1 << (format_bits - 1)) - 1;
int bps = format_bits / 8; // bytes per sample
int phys_bps = snd_pcm_format_physical_width(format) / 8;
int big_endian = snd_pcm_format_big_endian(format) == 1;
int to_unsigned = snd_pcm_format_unsigned(format) == 1;
int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
format == SND_PCM_FORMAT_FLOAT_BE);
float amplitude_scale = amplitude/8.56;
// verify and prepare the contents of areas
for (chn = 0; chn < channels; chn++) {
if ((areas[chn].first % 8) != 0) {
printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
exit(EXIT_FAILURE);
}
samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
if ((areas[chn].step % 16) != 0) {
printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
exit(EXIT_FAILURE);
}
steps[chn] = areas[chn].step / 8;
samples[chn] += offset * steps[chn];
}
// fill the channel areas
while (count-- > 0) {
union {
float f;
int i;
} fval;
int res, i;
if (is_float) {
fval.f = amplitude_scale * sin(phase) * maxval;
res = fval.i;
} else
res = amplitude_scale * sin(phase) * maxval;
if (to_unsigned)
res ^= 1U << (format_bits - 1);
for (chn = 0; chn < channels; chn++) {
// Generate data in native endian format
if (big_endian) {
for (i = 0; i < bps; i++)
*(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
} else {
for (i = 0; i < bps; i++)
*(samples[chn] + i) = (res >> i * 8) & 0xff;
}
samples[chn] += steps[chn];
}
phase += step;
if (phase >= max_phase)
phase -= max_phase;
}
*_phase = phase;
}......some code...
wave.cpp:
...(some code)...
void wave::on_pushButton_clicked()
{
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
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); ..................calling function
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\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
break; /* skip one period */
}
ptr += err * channels;
cptr -= err;
}
}...(some code)...
wave.h:
declaring function:
....(some code)...
class wave : public QMainWindow
{
Q_OBJECT
public:
explicit wave(QWidget *parent = 0);
~wave();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::wave *ui;
static int generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase) ;
};
I have created a program with QMainWindow on Qt Creator called wave and I have a function called generate_sine() in my main.cpp which I wish to call in my wave.cpp under my pushButton function. How do I do this?
What I tried was: I defined the function in main.cpp, I declared it in the header file(wave.h), and I tried to call/implement it in my wave file(wave.cpp) under the function: on_pushButton_clicked(). This is the way to do it in c++ programming, but its not working. Any help would be greatly appreciated.
Code is below for viewing:
main.cpp:
defining function:
...(some code)...
static int generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase)
{
static double max_phase = 2. * M_PI;
double phase = *_phase;
double step = max_phase*freq/(double)rate;
unsigned char *samples[channels];
int steps[channels];
unsigned int chn;
int format_bits = snd_pcm_format_width(format);
unsigned int maxval = (1 << (format_bits - 1)) - 1;
int bps = format_bits / 8; // bytes per sample
int phys_bps = snd_pcm_format_physical_width(format) / 8;
int big_endian = snd_pcm_format_big_endian(format) == 1;
int to_unsigned = snd_pcm_format_unsigned(format) == 1;
int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
format == SND_PCM_FORMAT_FLOAT_BE);
float amplitude_scale = amplitude/8.56;
// verify and prepare the contents of areas
for (chn = 0; chn < channels; chn++) {
if ((areas[chn].first % 8) != 0) {
printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
exit(EXIT_FAILURE);
}
samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
if ((areas[chn].step % 16) != 0) {
printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
exit(EXIT_FAILURE);
}
steps[chn] = areas[chn].step / 8;
samples[chn] += offset * steps[chn];
}
// fill the channel areas
while (count-- > 0) {
union {
float f;
int i;
} fval;
int res, i;
if (is_float) {
fval.f = amplitude_scale * sin(phase) * maxval;
res = fval.i;
} else
res = amplitude_scale * sin(phase) * maxval;
if (to_unsigned)
res ^= 1U << (format_bits - 1);
for (chn = 0; chn < channels; chn++) {
// Generate data in native endian format
if (big_endian) {
for (i = 0; i < bps; i++)
*(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
} else {
for (i = 0; i < bps; i++)
*(samples[chn] + i) = (res >> i * 8) & 0xff;
}
samples[chn] += steps[chn];
}
phase += step;
if (phase >= max_phase)
phase -= max_phase;
}
*_phase = phase;
}......some code...
wave.cpp:
...(some code)...
void wave::on_pushButton_clicked()
{
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
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); ..................calling function
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\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
break; /* skip one period */
}
ptr += err * channels;
cptr -= err;
}
}...(some code)...
wave.h:
declaring function:
....(some code)...
class wave : public QMainWindow
{
Q_OBJECT
public:
explicit wave(QWidget *parent = 0);
~wave();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::wave *ui;
static int generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase) ;
};