The right way to load audio files to Memory then play it?


#1

Im Not sure …
So please tell me if I do anything wrong:


class PlaySamplesLoop :public AudioProcessor
{
public:
    PlaySamplesLoop(File& SampleFlie)
    {
        this->WhereToStart=0;
        AudioFormatManager B;
        AudioFormatReader* reader=B.createReaderFor (SampleFlie);
        this->theSample.readFromAudioReader(reader,......);
        delete AudioFormatReader;
    }
    ..................
    AudioSampleBuffer theSample;
     .........
     .........
    void processBlock (AudioSampleBuffer &buffer, MidiBuffer &midiMessages)
    {
        /*  play the loaded sample again and again....*/
        float** A=buffer.getArrayOfChannels(); 
        float** B=theSample.getArrayOfChannels();
        for(int i=0;i<buffer.getNumSamples();++i) {
            for(int channel=0;channel<buffer.getNumChannels ();++channel) {
                A[channel][i]=B[0][WhereToStart];  //I guarantee that theSample will always be mono
            }
        WhereToStart=( WhereToStart==theSample.getNumSamples() ) ? 0 : WhereToStart+1;
    }
    .......
private:
    int WhereToStart;
}

...........
...........
AudioProcessor * MyProcessor=new PlaySamplesLoop("C:\1.wav"); //I'm sure there have a wav named "1" in here...


AudioProcessorPlayer player;
player.setProcessor (MyProcessor);


AudioDeviceManager audioDeviceManager ;
AudioIODevice * theDevice=audioDeviceManager.getCurrentAudioDevice();
theDevice->start (player);