Results 1 to 4 of 4

Thread: sine wave not generated with click of pushButton

  1. #1
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default sine wave not generated with click of pushButton

    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 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: sine wave not generated with click of pushButton

    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.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sine wave not generated with click of pushButton

    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

  4. #4
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: sine wave not generated with click of pushButton

    Quote Originally Posted by Ginsengelf View Post
    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.

Similar Threads

  1. draw wave problem
    By lzpmail in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th April 2011, 11:31
  2. Right-Click menu on a pushButton doesn't show up
    By qtpyqt in forum Qt Programming
    Replies: 6
    Last Post: 9th September 2010, 17:58
  3. twice execution of click on pushbutton?
    By rambo83 in forum Newbie
    Replies: 8
    Last Post: 10th November 2009, 12:21
  4. pushButton click event
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 11:19
  5. Working out table's current row from pushbutton click
    By shooogun in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2008, 23:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.