c++ - Assignment of local variables causes Audio to stop processing in JUCE -


edit: turned out uninitialized variable creating chaotic behavior. see this post getting more compiler warnings juce

i attempting create basic synthesizer , ran absurd problem when attempting assign value newly declared variable. after following along juce simple sine synthesis tutorial ran problem. basic code of getnextaudioblock() function when producing white noise. note how there 4 integers declared , assigned throughout:

const int numchannels = buffertofill.buffer->getnumchannels(); const int numsamples = buffertofill.numsamples; (int channel = 0; channel < numchannels; channel++){     float* const buffer = buffertofill.buffer -> getwritepointer(channel, buffertofill.startsample);     (int sample; sample < numsamples; sample++){         buffer[sample] = (randomgen.nextfloat() * 2.0f - 1.0f);     } } 

however, attempt add int no longer sound. adding line int unusedvariable = 0; anywhere in getnextaudioblock() function before buffer[sample] assignment returns function , therefore produces no audio.

if declare new variable (int unusedvariable;) still works. assignment part causes error. also, if declare variable global member assignment within function works fine.

to reiterate, works:

buffer[sample] = (randomgen.nextfloat() * 2.0f - 1.0f; 

this works:

int unusedvariable; buffer[sample] = (randomgen.nextfloat() * 2.0f - 1.0f; 

but doesn't:

int unusedvariable = 0; buffer[sample] = (randomgen.nextfloat() * 2.0f - 1.0f; 

my idea allocating new memory on audio thread causes error have seen declaration , assignment done in other online sources , in exact same function numchannels, numsamples, channel, , sample allocated , assigned fine. considered has using random class, same problem when generating sine waves.

edit: here exact code copied project. right here nextsample declared globally, buffer not filled when declared locally

  void maincontentcomponent::getnextaudioblock (const audiosourcechannelinfo& buffertofill)   {     const int numchannels = buffertofill.buffer->getnumchannels();     const int numsamples = buffertofill.numsamples;     (int channel = 0; channel < numchannels; channel++){         float* const buffer = buffertofill.buffer -> getwritepointer (channel, buffertofill.startsample);         (int sample; sample < numsamples; sample++){             // nextsample = (randomgen.nextfloat() * 2.0f - 1.0f); // randomly generated white noise             nextsample = (float) std::sin (currentangle);             currentangle += angledelta;             buffer[sample] = nextsample * volumelevel;         }     }   } 

i created new audioapplication project in projucer , pasted block of code getnextaudioblock() method (adding sensible member variables you're referencing them here).

the compiler pointed @ problem right away -- loop variable sample below isn't initialized (and c++ won't default init you), if memory used variable happened have contained value that's less buffer size, you'll generate audio; if not, buffer passed function unaffected because loop never runs.

    (int sample; sample < numsamples; sample++){         nextsample = (randomgen.nextfloat() * 2.0f - 1.0f); // randomly generated white noise         //nextsample = (float) std::sin (currentangle);         //currentangle += angledelta;         buffer[sample] = nextsample * volumelevel;     } 

see if changing for (int sample=0; doesn't fix things you.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -