I am having difficulties loading file to a audio plug in.
first of all i have a button and when i click it i use Filechooser to select the file.
Next I call processor function loadFile to read the file.
void TESTAudioProcessorEditor::buttonClicked(Button* button)
{
if (button == &openFile)
{
FileChooser chooser(“Select a Wav file to play…”,
File::getSpecialLocation(File::userHomeDirectory),
"*.wav");
if (chooser.browseForFileToOpen())
{
File file(chooser.getResult());
processor.loadFile(file);
}
}
}
void TESTAudioProcessor::loadFile(File& file)
{
ScopedPointer<AudioFormatReader>reader = formatManager.createReaderFor(file);
if (reader != nullptr && dataBuffer != nullptr)
{
int total = reader->lengthInSamples;
dataBuffer->setSize(reader->numChannels, total);
reader->read(dataBuffer, 0, total, 0, true, true);
}
}
But whenever i try to debug seems that never enters the if statement because databuffer is null.
and i get that exception triggered if (newNumSamples != size || newNumChannels != numChannels)
Thnak you again for the quick response .
So these are my files in my pluginprocessor.cpp I have in the constructor
dataBuffer = new AudioSampleBuffer();.
But I think that the real problem is the way I load my file because when I debug my code loaded files doesnot have proper structure .
It 's my first plugin loading files and play them .So please be gentle
Ok -
So this is like a bare-bones sample player at the moment?
I don’t know why you’re getting your data buffer as a nullptr at that line. You are setting it up in the constructor, and then using it in a function that will be called after the constructor has completed. And at no point are you assigning it to null.
However you do have a possible threading issue that could cause a crash. You have the UI thread updating the AudIoSampleBuffer size whilst the audio thread is using the buffer.
I guess there are a few ways around that problem, but some sort of lock will be a cheap fix.
You don’t want to block on the audio thread though. So using a CriticalSection with the tryEnter() method inside processBlock() (and the exit method), and then use a ScopedLock in your loadFile() method (which can block).
Before that in the PluginProcessor.cpp my reader->read(dataBuffer, 0, total, 0, true, false);has these arguments and
databuffer has these values inside {object=0x00c8a750 {numChannels=1 size=986473 allocatedBytes=3945952 ...} }
So it seems that databuffer has the right values, but sourceChannel, and source.numChannels even though are equal still getting that error.
Thank you for you help , yes first I wanted to load stereo files and I left the buffer handling stereo files …
I managed to play sound but when I choose another file crushes .I find it very difficult to manage the thread problem for now since I don’t have so much experience .
Another thing I want to do is to have a midi controller to control the playback is it possible ?Can I control when the audio file is played or stopped through a midi controller ?I could use some example code if you can .Because I am lookngjuce tutorials andjuce demo - the tutorials are very specific and thejuce demo is just huge Thank you again.!!!
There’s a bug in the code I posted. It lets the pointer increment even when the sample isn’t playing. If you leave it running overnight you’ll get an integer overflow. The fix is to either use an int64 for the sample pointer, or to not increment the value when the sample has reached the end, like this:
Thank you bazrush that worked .It is a little bit tricky for someone with basic audio programming knowledge but I think eventually I ll manage to understand it .Thanks again.