Results 1 to 4 of 4

Thread: trying to call function from main.cpp to wave.cpp

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

    Default trying to call function from main.cpp to wave.cpp

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

    wave.cpp:

    Qt Code:
    1. ...(some code)...
    2. void wave::on_pushButton_clicked()
    3. {
    4. freq = ui->frequency->text().toDouble();
    5. ampl = ui->amplitude->text().toDouble();
    6. snd_pcm_t *handle;
    7. signed short *samples;
    8. snd_pcm_channel_area_t *areas;
    9. double phase = 0;
    10. signed short *ptr;
    11. int err, cptr;
    12.  
    13. while (isTrue) {
    14. generate_sine(areas, 0, period_size, &phase); .................[B].calling function[/B]
    15.  
    16. ptr = samples;
    17. cptr = period_size;
    18. while (cptr > 0) {
    19. err = snd_pcm_writei(handle, ptr, cptr);
    20. if (err == -EAGAIN)
    21. continue;
    22. if (err < 0) {
    23. if (xrun_recovery(handle, err) < 0) {
    24. printf("Write error: %s\n", snd_strerror(err));
    25. exit(EXIT_FAILURE);
    26. }
    27. break; /* skip one period */
    28. }
    29. ptr += err * channels;
    30. cptr -= err;
    31. }
    32. }...(some code)...
    To copy to clipboard, switch view to plain text mode 

    wave.h:
    declaring function:
    Qt Code:
    1. ....(some code)...
    2. class wave : public QMainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit wave(QWidget *parent = 0);
    8. ~wave();
    9.  
    10. private slots:
    11. void on_pushButton_clicked();
    12.  
    13. void on_pushButton_2_clicked();
    14. private:
    15. Ui::wave *ui;
    16. static int generate_sine(const snd_pcm_channel_area_t *areas,
    17. snd_pcm_uframes_t offset,
    18. int count, double *_phase) ;
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by duma; 5th August 2011 at 20:31.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: trying to call function from main.cpp to wave.cpp

    declare the function in header "generate_sine.h":
    Qt Code:
    1. // generate_sine.h
    2.  
    3. #include something
    4.  
    5. extern void generate_sine( const snd_pcm_channel_area_t *areas,
    6. snd_pcm_uframes_t offset,
    7. int count, double *_phase );
    To copy to clipboard, switch view to plain text mode 
    and put the implementation in "generate_sine.cpp" (without the "static" keyword).
    Add the sources to .pro file.
    Then include "generate_sine.h" in "main.cpp" or wherever else you want to use the method.

  3. The following user says thank you to stampede for this useful post:

    duma (5th August 2011)

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

    Default Re: trying to call function from main.cpp to wave.cpp

    Quote Originally Posted by stampede View Post
    declare the function in header "generate_sine.h":
    Qt Code:
    1. // generate_sine.h
    2.  
    3. #include something
    4.  
    5. extern void generate_sine( const snd_pcm_channel_area_t *areas,
    6. snd_pcm_uframes_t offset,
    7. int count, double *_phase );
    To copy to clipboard, switch view to plain text mode 
    and put the implementation in "generate_sine.cpp" (without the "static" keyword).
    Add the sources to .pro file.
    Then include "generate_sine.h" in "main.cpp" or wherever else you want to use the method.
    Hi,
    I tried this, it didn't work. Still getting the same error:
    Qt Code:
    1. undefined reference to `wave::generate_sine(_snd_pcm_channel_area const*, unsigned long, int, double*)
    To copy to clipboard, switch view to plain text mode 

    generate_sine.cpp:
    Qt Code:
    1. #include <stdio.h>
    2. #include <cstdio>
    3. #include <stdlib.h>
    4. #include <cstdlib>
    5. #include <string.h>
    6. #include <cstring>
    7. #include <sched.h>
    8. #include <errno.h>
    9. #include <getopt.h>
    10. #include <alsa/asoundlib.h>
    11. #include <sys/time.h>
    12. #include <math.h>
    13. #include <cmath>
    14. #include <iostream>
    15. using namespace std;
    16.  
    17. static const char *device = "plughw:0,0"; // playback device
    18. static snd_pcm_format_t format = SND_PCM_FORMAT_S16; // sample format- change to 24-bit
    19. static unsigned int rate = 96000; // stream rate
    20. static unsigned int channels = 128; // count of channels
    21. static unsigned int buffer_time = 500000; // ring buffer length in us
    22. static unsigned int period_time = 100000; // period time in us
    23. static double freq; // sinusoidal wave frequency in Hz
    24. static int verbose = 0; // verbose flag
    25. static int resample = 1; // enable alsa-lib resampling
    26. static int period_event = 0; // produce poll event after each period
    27. static snd_pcm_sframes_t buffer_size;
    28. static snd_pcm_sframes_t period_size;
    29. static snd_output_t *output = NULL;
    30. static double ampl;
    31. static snd_pcm_t *h;
    32. static double amplitude;
    33.  
    34.  
    35. int generate_sine(const snd_pcm_channel_area_t *areas,
    36. snd_pcm_uframes_t offset,
    37. int count, double *_phase)
    38.  
    39. {
    40.  
    41. static double max_phase = 2. * M_PI;
    42. double phase = *_phase;
    43. double step = max_phase*freq/(double)rate;
    44. unsigned char *samples[channels];
    45. int steps[channels];
    46. unsigned int chn;
    47. int format_bits = snd_pcm_format_width(format);
    48. unsigned int maxval = (1 << (format_bits - 1)) - 1;
    49. int bps = format_bits / 8; /* bytes per sample */
    50. int phys_bps = snd_pcm_format_physical_width(format) / 8;
    51. int big_endian = snd_pcm_format_big_endian(format) == 1;
    52. int to_unsigned = snd_pcm_format_unsigned(format) == 1;
    53. int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
    54. format == SND_PCM_FORMAT_FLOAT_BE);
    55.  
    56. float amplitude_scale = amplitude/8.56;
    57.  
    58. /* verify and prepare the contents of areas */
    59. for (chn = 0; chn < channels; chn++) {
    60. if ((areas[chn].first % 8) != 0) {
    61. printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
    62. exit(EXIT_FAILURE);
    63. }
    64. samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
    65. if ((areas[chn].step % 16) != 0) {
    66. printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
    67. exit(EXIT_FAILURE);
    68. }
    69. steps[chn] = areas[chn].step / 8;
    70. samples[chn] += offset * steps[chn];
    71. }
    72. /* fill the channel areas */
    73. while (count-- > 0) {
    74. union {
    75. float f;
    76. int i;
    77. } fval;
    78. int res, i;
    79. if (is_float) {
    80. fval.f = amplitude_scale * sin(phase) * maxval;
    81. res = fval.i;
    82. } else
    83. res = amplitude_scale * sin(phase) * maxval;
    84. if (to_unsigned)
    85. res ^= 1U << (format_bits - 1);
    86. for (chn = 0; chn < channels; chn++) {
    87. /* Generate data in native endian format */
    88. if (big_endian) {
    89. for (i = 0; i < bps; i++)
    90. *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
    91. } else {
    92. for (i = 0; i < bps; i++)
    93. *(samples[chn] + i) = (res >> i * 8) & 0xff;
    94. }
    95. samples[chn] += steps[chn];
    96. }
    97. phase += step;
    98. if (phase >= max_phase)
    99. phase -= max_phase;
    100. }
    101. *_phase = phase;
    102. }
    To copy to clipboard, switch view to plain text mode 
    generate_sine.h:
    Qt Code:
    1. #ifndef GENERATE_SINE_H
    2. #define GENERATE_SINE_H
    3. #include <alsa/asoundlib.h>
    4. #include <QMainWindow>
    5. #include <QObject>
    6.  
    7. extern void generate_sine(const snd_pcm_channel_area_t *areas,
    8. snd_pcm_uframes_t offset,
    9. int count, double *_phase );
    10.  
    11. #endif // GENERATE_SINE_H
    To copy to clipboard, switch view to plain text mode 


    wave.pro:
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = Wave
    4. TEMPLATE = app
    5.  
    6.  
    7. SOURCES += main.cpp\
    8. wave.cpp \
    9. generate_sine.cpp
    10.  
    11. HEADERS += wave.h \
    12. generate_sine.h
    13.  
    14.  
    15. LIBS += -lasound
    16.  
    17.  
    18. FORMS += wave.ui
    To copy to clipboard, switch view to plain text mode 


    Added after 20 minutes:


    Quote Originally Posted by stampede View Post
    declare the function in header "generate_sine.h":
    Qt Code:
    1. // generate_sine.h
    2.  
    3. #include something
    4.  
    5. extern void generate_sine( const snd_pcm_channel_area_t *areas,
    6. snd_pcm_uframes_t offset,
    7. int count, double *_phase );
    To copy to clipboard, switch view to plain text mode 
    and put the implementation in "generate_sine.cpp" (without the "static" keyword).
    Add the sources to .pro file.
    Then include "generate_sine.h" in "main.cpp" or wherever else you want to use the method.
    Hey thanks for the reply. This does allow me to call it in other files, but it doesn't let me call it under the on_pushButton_clicked() function. It gives me the error:
    Qt Code:
    1. undefined reference to `wave::generate_sine(_snd_pcm_channel_area const*, unsigned long, int, double*)
    To copy to clipboard, switch view to plain text mode 
    Last edited by duma; 5th August 2011 at 22:19.

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: trying to call function from main.cpp to wave.cpp

    Qt Code:
    1. undefined reference to `wave::generate_sine
    To copy to clipboard, switch view to plain text mode 
    Have you declared this method elsewhere ? For example, in wave namespace (or class) ? Can you show how you want to use it ?

Similar Threads

  1. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00
  2. Call to database function.
    By retto in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 12:29
  3. call function as Qstring
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 01:35
  4. call variable from other function
    By walito in forum Newbie
    Replies: 7
    Last Post: 31st August 2007, 18:36
  5. function call
    By Walsi in forum Qt Programming
    Replies: 3
    Last Post: 12th June 2007, 09:13

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
  •  
Qt is a trademark of The Qt Company.