Results 1 to 2 of 2

Thread: Re: square wave generating in terminal but not in Qt Creator[SOLVED]

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

    Default Re: square wave generating in terminal but not in Qt Creator[SOLVED]

    Hi guys.
    I have a program that generates a sine wave and a square wave using user inputs of frequency and amplitude. When I run the program in terminal in linux with a g++ compiler, it runs perfectly.
    But when I run it in Qt Creator, the sine wave runs perfectly, but the square wave doesn't, it has gaps i.e. it produces square waves for a short amount of time and then no waves for another short amount of time and then square waves again etc. However, this is not in a periodic fashion.
    I'm not sure why the same code is producing the waves fine in terminal but not in Qt Creator. Any help would be greatly appreciated.

    The way I am running both waves is through the use of threads to start and terminate each wave with the use of pushButtons. My code is below:

    generates the square wave:
    Qt Code:
    1. static void generate_square(snd_pcm_uframes_t offset, int count, double *_phase)
    2. {
    3. static double max_phase = 2. * M_PI;
    4. double phase = *_phase;
    5. double step = max_phase*freq/(double)rate;
    6. unsigned char *samples[channels];
    7. int steps[channels];
    8. unsigned int chn;
    9. int format_bits = snd_pcm_format_width(format);
    10. unsigned int maxval = (1 << (format_bits - 1)) - 1;
    11. int bps = format_bits / 8; /* bytes per sample */
    12. int phys_bps = snd_pcm_format_physical_width(format) / 8;
    13. int big_endian = snd_pcm_format_big_endian(format) == 1;
    14. int to_unsigned = snd_pcm_format_unsigned(format) == 1;
    15. int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
    16. format == SND_PCM_FORMAT_FLOAT_BE);
    17. float amplitude_scale = ampl/8.56;
    18. float a;
    19. int b;
    20.  
    21. // verify and prepare the contents of areas
    22. for (chn = 0; chn < channels; chn++) {
    23.  
    24. if ((areas[chn].first % 8) != 0) {
    25.  
    26. printf("areas[%i].first == %i, aborting...", chn , areas[chn].first);
    27. exit(EXIT_FAILURE);
    28. }
    29. samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
    30.  
    31.  
    32. if ((areas[chn].step % 16) != 0) {
    33.  
    34. // printf("areas[%i].step == %i, aborting... ", chn areas[chn].step);
    35. exit(EXIT_FAILURE);
    36. }
    37. steps[chn] = areas[chn].step / 8;
    38. samples[chn] += offset * steps[chn];
    39.  
    40.  
    41. }
    42. // fill the channel areas
    43. while (count-- > 0) {
    44. union {
    45. float f;
    46. int i;
    47. } fval;
    48. int res, i;
    49. if (is_float) {
    50. a = amplitude_scale * sgn(sin(phase)) * maxval;
    51. fval.f = a;
    52. res = fval.i;
    53. } else {
    54. b = amplitude_scale * sgn(sin(phase)) * maxval;
    55. printf("b = %d\n",b);
    56. res = b;
    57.  
    58. }
    59.  
    60. if (to_unsigned)
    61. res ^= 1U << (format_bits - 1);
    62. for (chn = 0; chn < channels; chn++) {
    63. // Generate data in native endian format
    64. if (big_endian) {
    65. for (i = 0; i < bps; i++)
    66. *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
    67. } else {
    68. for (i = 0; i < bps; i++)
    69. *(samples[chn] + i) = (res >> i * 8) & 0xff;
    70. }
    71. samples[chn] += steps[chn];
    72. }
    73. phase += step;
    74. if (phase >= max_phase)
    75. phase -= max_phase;
    76. }
    77. *_phase = phase;
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 

    produces/writes the square wave:
    Qt Code:
    1. void onWriteLoopSquare()
    2. {
    3.  
    4.  
    5. double phase = 0;
    6. signed short *ptr;
    7. int err, cptr;
    8.  
    9. generate_square(0, period_size, &phase);
    10.  
    11. while (1) {
    12.  
    13. ptr = samples;
    14. cptr = period_size;
    15. while (cptr > 0) {
    16. err = snd_pcm_writei(hspdif, ptr, cptr);
    17. if (err == -EAGAIN)
    18. continue;
    19. if (err < 0) {
    20. if (xrun_recovery(hspdif, err) < 0) {
    21. printf("Write error: %s ", snd_strerror(err));
    22. exit(EXIT_FAILURE);
    23. }
    24. break; // skip one period
    25. }
    26. ptr += err * channels;
    27. cptr -= err;
    28. }
    29. }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    pushButton function:
    Qt Code:
    1. void wave::on_goSquare_clicked()
    2. {
    3. //Generate
    4. freq = ui->squareFrequency->text().toDouble();
    5. ampl = ui->squareAmplitude->text().toDouble();
    6.  
    7. squarethread->start();
    8. ui->goSquare->setEnabled(false);
    9. ui->goSquare->setStyleSheet("background-color: gray");
    10. ui->stopSquare->setStyleSheet("background-color: rgb(255, 192, 192);"
    11. "color: red;");
    12. ui->stopSquare->setEnabled(true);
    13. }
    14.  
    15. void wave::on_stopSquare_clicked()
    16. {
    17. //Terminate
    18. squarethread->terminate();
    19. ui->stopSquare->setEnabled(false);
    20. ui->stopSquare->setStyleSheet("background-color: gray");
    21. ui->goSquare->setStyleSheet("background-color: rgb(192, 255, 208);"
    22. "color: green;");
    23. ui->goSquare->setEnabled(true);
    24. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 15 minutes:


    never mind. found my mistake. I took float a and int b out of my program so there were no delays assigning the calculations to fval.f and res in function generate_square()
    Last edited by duma; 18th August 2011 at 18:16.

  2. #2
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: square wave generating in terminal but not in Qt Creator[SOLVED]

    Quote Originally Posted by duma View Post
    Hi guys.
    I have a program that generates a sine wave and a square wave using user inputs of frequency and amplitude. When I run the program in terminal in linux with a g++ compiler, it runs perfectly.
    But when I run it in Qt Creator, the sine wave runs perfectly, but the square wave doesn't, it has gaps i.e. it produces square waves for a short amount of time and then no waves for another short amount of time and then square waves again etc. However, this is not in a periodic fashion.
    I'm not sure why the same code is producing the waves fine in terminal but not in Qt Creator. Any help would be greatly appreciated.

    The way I am running both waves is through the use of threads to start and terminate each wave with the use of pushButtons. My code is below:

    generates the square wave:
    Qt Code:
    1. static void generate_square(snd_pcm_uframes_t offset, int count, double *_phase)
    2. {
    3. static double max_phase = 2. * M_PI;
    4. double phase = *_phase;
    5. double step = max_phase*freq/(double)rate;
    6. unsigned char *samples[channels];
    7. int steps[channels];
    8. unsigned int chn;
    9. int format_bits = snd_pcm_format_width(format);
    10. unsigned int maxval = (1 << (format_bits - 1)) - 1;
    11. int bps = format_bits / 8; /* bytes per sample */
    12. int phys_bps = snd_pcm_format_physical_width(format) / 8;
    13. int big_endian = snd_pcm_format_big_endian(format) == 1;
    14. int to_unsigned = snd_pcm_format_unsigned(format) == 1;
    15. int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
    16. format == SND_PCM_FORMAT_FLOAT_BE);
    17. float amplitude_scale = ampl/8.56;
    18. float a;
    19. int b;
    20.  
    21. // verify and prepare the contents of areas
    22. for (chn = 0; chn < channels; chn++) {
    23.  
    24. if ((areas[chn].first % 8) != 0) {
    25.  
    26. printf("areas[%i].first == %i, aborting...", chn , areas[chn].first);
    27. exit(EXIT_FAILURE);
    28. }
    29. samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
    30.  
    31.  
    32. if ((areas[chn].step % 16) != 0) {
    33.  
    34. // printf("areas[%i].step == %i, aborting... ", chn areas[chn].step);
    35. exit(EXIT_FAILURE);
    36. }
    37. steps[chn] = areas[chn].step / 8;
    38. samples[chn] += offset * steps[chn];
    39.  
    40.  
    41. }
    42. // fill the channel areas
    43. while (count-- > 0) {
    44. union {
    45. float f;
    46. int i;
    47. } fval;
    48. int res, i;
    49. if (is_float) {
    50. a = amplitude_scale * sgn(sin(phase)) * maxval;
    51. fval.f = a;
    52. res = fval.i;
    53. } else {
    54. b = amplitude_scale * sgn(sin(phase)) * maxval;
    55. printf("b = %d\n",b);
    56. res = b;
    57.  
    58. }
    59.  
    60. if (to_unsigned)
    61. res ^= 1U << (format_bits - 1);
    62. for (chn = 0; chn < channels; chn++) {
    63. // Generate data in native endian format
    64. if (big_endian) {
    65. for (i = 0; i < bps; i++)
    66. *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
    67. } else {
    68. for (i = 0; i < bps; i++)
    69. *(samples[chn] + i) = (res >> i * 8) & 0xff;
    70. }
    71. samples[chn] += steps[chn];
    72. }
    73. phase += step;
    74. if (phase >= max_phase)
    75. phase -= max_phase;
    76. }
    77. *_phase = phase;
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 
    hello,I just don't understand the hardware situation of the program,especially the first part code with the name:generates the square wave
    could you please give me your E-mail address?
    thanks a lot.
    Last edited by jetsu; 11th April 2012 at 08:59.

Similar Threads

  1. Replies: 3
    Last Post: 5th August 2011, 22:39
  2. draw wave problem
    By lzpmail in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th April 2011, 11:31
  3. Replies: 0
    Last Post: 2nd March 2011, 08:36
  4. Getting sound data from a wave file
    By ko9 in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 11:23
  5. Replies: 8
    Last Post: 2nd August 2006, 16:19

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.