A Question on the using of TimeSliceThread in AudioTransportSource::setSource


#1

Recently I’m trying to use JUCE to play audio.

After one day and a half I still can’t successfully made it. The related code is:


/*    MyWindow.h    */

class MyWindow : public DocumentWindow

{

public:

    static MyWindow* ptr;

    MyWindow();

    void closeButtonPressed() override;

    void paint(Graphics&) override;

 

private:

    Font f;

 

    AudioFormatManager* afm;

    AudioFormatReader* afr;

    AudioFormatReaderSource* afrs;

    AudioTransportSource* ats;

    AudioDeviceManager* adm;

    AudioSourcePlayer* asp;

 

    TimeSliceThread* tst;

 

    JUCE_DECLARE_NON_COPYABLE(MyWindow);

};


/*    MyWindow.cpp    */

MyWindow::MyWindow(): DocumentWindow("GUI Test", Colours::lightgrey, DocumentWindow::minimiseButton | DocumentWindow::closeButton), f("Times New Roman", 20, Font::plain), afm(0), afr(0), afrs(0), ats(0), adm(0), asp(0), tst(0)

{

    setUsingNativeTitleBar(1);

    setVisible(1);

 

    Desktop::getInstance().setGlobalScaleFactor(1.0);

    centreWithSize(640, 480);

    //=============================================

    afm=new AudioFormatManager();

    afm->registerBasicFormats();

 

    afr=afm->createReaderFor(File("test.ogg"));

 

    afrs=new AudioFormatReaderSource(afr, 1);

    afrs->setLooping(1);

 

    tst=new TimeSliceThread("testThread");

 

    ats=new AudioTransportSource();

    ats->setSource(afrs, 2^15, tst, afr->sampleRate, 2);    //******************

    ats->setPosition(0.);

 

    asp=new AudioSourcePlayer();

    asp->setSource(ats);

 

    adm=new AudioDeviceManager();

    adm->initialiseWithDefaultDevices(0, 2);

    adm->addAudioCallback(asp);

 

    ats->start();

 

    return;

}


And I tried to replace the statement in constructor MyWindow::MyWindow()

“ats->setSource(afrs, 2^15, tst, afr->sampleRate, 2);” with

“ats->setSource(afrs, 0, 0, afr->sampleRate, 2);” (the line with lots of “*” in comment), then I found it could successfully work.

So I know that I have using the TimeSliceThread class in wrong way.

And the question is how I can correctly use the class TimeSliceThread in AudioTransportSource::setSource? Thanks.


#2

What are you actually trying to do with the TimeSliceThread?


#3

I want to use TimeSliceThread to buffer the audio data in another thread.

And finally I find out the bug is I've forgotten startThread().