QMainWIndow not popping up when I run
-
I'm not aware of using low-level sound api, but transfer_loop is looking like it will be looping until something happens.
-
It's probably terminating because you're deleting all of your resources before you ever call a.exec(). (See lines 86-88 in your original code posting.)
If you'll pardon my candor, it looks like you've cut-and-pasted a C example from ALSA into your main() and have made a few tweaks to replace "calloc" with "new", and are just kind of expecting it to seamlessly integrate with Qt. I think that you're going to keep running into compilation and execution issues because you might not understand the nuances of the code that you're trying to use, nor the way that the event loop and everything works under Qt.
Are there more fundamental issues with C++ and/or Qt that you may need to focus on further beforehand that you might still be a little uncertain of? If there are, it may help to ask about those issues first.
-
[quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
printf("Transfer failed: %s\n", snd_strerror(err));@This is the line of code that stops my window from popping up. I don't know why. Any help would be greatly appreciated.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
-
[quote author="Gerolf" date="1312870616"][quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
printf("Transfer failed: %s\n", snd_strerror(err));@This is the line of code that stops my window from popping up. I don't know why. Any help would be greatly appreciated.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
[/quote]
Yes. You are right. Thank you. I removed the line from my int main() as I am calling it under my pushButton function to display the sine wave. My window pops up now but when I click the push button, my program terminates. i'm not sure why. this is my push button function:
@void wave::on_pushButton_clicked()
{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; } } }
@
-
Did you check, whether line 25 is executed?
Did you try to debug it and step through to check what happens?
You have an endless loop here (where is isTrue set to false?)
If you provide a full, runnable and debuggable example, perhaps others could also try with the debugger...
-
[quote author="ogopa" date="1312895011"]
[quote author="Gerolf" date="1312870616"][quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
printf("Transfer failed: %s\n", snd_strerror(err));@This is the line of code that stops my window from popping up. I don't know why. Any help would be greatly appreciated.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
[/quote]
Yes. You are right. Thank you. I removed the line from my int main() as I am calling it under my pushButton function to display the sine wave. My window pops up now but when I click the push button, my program terminates. i'm not sure why. this is my push button function:
@void wave::on_pushButton_clicked()
{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; } } }
@
[/quote]
actually just solved this problem, there was code in my program that was terminating it. So everything compiles without errors and runs perfectly. Just when I input values of frequency and amplitude now, it doesn't output any sine wave :( . Why does my push button function not generate a sine wave. It does generate a sine wave when I run it in terminal. Any help will be appreciated.
-
[quote author="mlong" date="1312839492"]It's probably terminating because you're deleting all of your resources before you ever call a.exec(). (See lines 86-88 in your original code posting.)
If you'll pardon my candor, it looks like you've cut-and-pasted a C example from ALSA into your main() and have made a few tweaks to replace "calloc" with "new", and are just kind of expecting it to seamlessly integrate with Qt. I think that you're going to keep running into compilation and execution issues because you might not understand the nuances of the code that you're trying to use, nor the way that the event loop and everything works under Qt.
Are there more fundamental issues with C++ and/or Qt that you may need to focus on further beforehand that you might still be a little uncertain of? If there are, it may help to ask about those issues first.
[/quote]
Hi mlong. I agree. Although, before I started integrating my code with Qt, I made sure that my converted code compiled with a c++ compiler in terminal i.e. g++ and ran fine to produce a sine wave. I am using Qt Creator to build a gui that lets me input frequency and amplitude and produces a sine wave with those specifications. I have now reached a stage in Qt where I have dealt with all the errors. I have no more errors, my QMainWindow pops up. Only now, the problem thhat I am having is that my pushButton function doesn't seem to be generating a sine wave.
I thank you for your help so far and I hope you will bear with me for just a little while longer. I feel like I am almost there. -
[quote author="Gerolf" date="1312900157"]# Did you check, whether line 25 is executed?
Did you try to debug it and step through to check what happens?
You have an endless loop here (where is isTrue set to false?)
If you provide a full, runnable and debuggable example, perhaps others could also try with the debugger...[/quote]
Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my 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.cpp:
@
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; } }
}@
-
generate_sine.cpp:
@
//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;void 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_phasefreq/(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...", 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... ", 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;
}
@ -
[quote author="ogopa" date="1312902815"][quote author="Gerolf" date="1312900157"]# Did you check, whether line 25 is executed?
Did you try to debug it and step through to check what happens?
You have an endless loop here (where is isTrue set to false?)
If you provide a full, runnable and debuggable example, perhaps others could also try with the debugger...[/quote]
Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my wave.cpp:
[/quote]Hi,
As the loop executed on a button press, which means runs inside the main event loop, tzhe second button press will never be executed, as the main thread is blocked.
Regarding the other thing, I have no idea, as I have no experience in sound card programming...
-
[quote author="Gerolf" date="1312906849"]
[quote author="ogopa" date="1312902815"][quote author="Gerolf" date="1312900157"]# Did you check, whether line 25 is executed?Did you try to debug it and step through to check what happens?
You have an endless loop here (where is isTrue set to false?)
If you provide a full, runnable and debuggable example, perhaps others could also try with the debugger...[/quote]
Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my wave.cpp:
[/quote]Hi,
As the loop executed on a button press, which means runs inside the main event loop, tzhe second button press will never be executed, as the main thread is blocked.
Regarding the other thing, I have no idea, as I have no experience in sound card programming...
[/quote]
ok. Thanks for your help. Much appreciated.