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:
Qt Code:
  1. #include <QtGui>
  2. #include <QApplication>
  3. #include <qlineedit.h>
  4. #include "ui_wave.h"
  5. #include <QString>
  6. #include <QObject>
  7. #include <QDebug>
  8. #include <stdio.h>
  9. #include <cstdio>
  10. #include <stdlib.h>
  11. #include <cstdlib>
  12. #include <string.h>
  13. #include <cstring>
  14. #include <sched.h>
  15. #include <errno.h>
  16. #include <getopt.h>
  17. #include <alsa/asoundlib.h>
  18. #include <sys/time.h>
  19. #include <sstream>
  20. #include <string>
  21. #include <math.h>
  22. #include <cmath>
  23. #include <iostream>
  24. #include "generate_sine.h"
  25. #include "xrun_recovery.h"
  26. #include "wave.h"
  27. #include "write_loop.h"
  28. using namespace std;
  29.  
  30.  
  31. static const char *device = "plughw:0,0"; // playback device
  32. static snd_pcm_format_t format = SND_PCM_FORMAT_S16; // sample format- change to 24-bit
  33. static unsigned int rate = 96000; // stream rate
  34. static unsigned int channels = 128; // count of channels
  35. static unsigned int buffer_time = 500000; // ring buffer length in us
  36. static unsigned int period_time = 100000; // period time in us
  37. static double freq; // sinusoidal wave frequency in Hz
  38. static int verbose = 0; // verbose flag
  39. static int resample = 1; // enable alsa-lib resampling
  40. static int period_event = 0; // produce poll event after each period
  41. static snd_pcm_sframes_t buffer_size;
  42. static snd_pcm_sframes_t period_size;
  43. static snd_output_t *output = NULL;
  44. static double amplitude;
  45. static bool isTrue;
  46.  
  47.  
  48. wave::wave(QWidget *parent) :
  49. QMainWindow(parent),
  50. ui(new Ui::wave)
  51. {
  52. ui->setupUi(this);
  53.  
  54.  
  55. }
  56. wave::~wave()
  57. {
  58. delete ui;
  59.  
  60. }
  61.  
  62.  
  63. void wave::on_pushButton_clicked()
  64. {
  65. snd_pcm_t *handle;
  66. snd_pcm_channel_area_t *areas;
  67. signed short *samples;
  68.  
  69. write_loop(handle, samples, areas);
  70.  
  71. }
  72.  
  73.  
  74.  
  75. void wave::on_pushButton_2_clicked()
  76. {
  77. isTrue = false;
  78.  
  79. }
To copy to clipboard, switch view to plain text mode 

write_loop():
Qt Code:
  1. #include <QtGui>
  2. #include <QApplication>
  3. #include <qlineedit.h>
  4. #include "ui_wave.h"
  5. #include <QString>
  6. #include <QObject>
  7. #include <QDebug>
  8. #include <stdio.h>
  9. #include <cstdio>
  10. #include <stdlib.h>
  11. #include <cstdlib>
  12. #include <string.h>
  13. #include <cstring>
  14. #include <sched.h>
  15. #include <errno.h>
  16. #include <getopt.h>
  17. #include <alsa/asoundlib.h>
  18. #include <sys/time.h>
  19. #include <sstream>
  20. #include <string>
  21. #include <math.h>
  22. #include <cmath>
  23. #include <iostream>
  24. #include "write_loop.h"
  25. #include "wave.h"
  26. #include "generate_sine.h"
  27. #include "xrun_recovery.h"
  28. using namespace std;
  29.  
  30. static char *device = "plughw:0,0"; /* playback device */
  31. static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
  32. static unsigned int rate = 96000; /* stream rate */
  33. static unsigned int channels = 128; /* count of channels */
  34. static unsigned int buffer_time = 500000; /* ring buffer length in us */
  35. static unsigned int period_time = 100000; /* period time in us */
  36. static double freq = 440; /* sinusoidal wave frequency in Hz */
  37. static int verbose = 0; /* verbose flag */
  38. static int resample = 1; /* enable alsa-lib resampling */
  39. static int period_event = 0; /* produce poll event after each period */
  40. static snd_pcm_sframes_t buffer_size;
  41. static snd_pcm_sframes_t period_size;
  42. static snd_output_t *output = NULL;
  43. static float amplitude;
  44. static bool isTrue;
  45.  
  46. int write_loop(snd_pcm_t *handle,
  47. signed short *samples,
  48. snd_pcm_channel_area_t *areas)
  49. {
  50. double phase = 0;
  51. signed short *ptr;
  52. int err, cptr;
  53.  
  54. while (isTrue) {
  55.  
  56. generate_sine(areas, 0, period_size, &phase);
  57. ptr = samples;
  58. cptr = period_size;
  59. while (cptr > 0) {
  60. err = snd_pcm_writei(handle, ptr, cptr);
  61.  
  62. if (err == -EAGAIN)
  63. continue;
  64. if (err < 0) {
  65. if (xrun_recovery(handle, err) < 0) {
  66. printf("Write error: %s ", snd_strerror(err));
  67. exit(EXIT_FAILURE);
  68. }
  69. break; /* skip one period */
  70. }
  71. ptr += err * channels;
  72. cptr -= err;
  73. }
  74. }
  75. }
To copy to clipboard, switch view to plain text mode