Problems with tabbed component


#1

I’m having issues loading things into tabs in a tabbed component. I’m using the “DemoTabbedComponent” class from the JUCE widgets demo:

DemoTabbedComponent::DemoTabbedComponent (ApplicationCommandManager* commandManager) : TabbedComponent (TabbedButtonBar::TabsAtTop) { addTab (T("Keys"), Colours::orchid, new KeyboardExercise(), true); }

There is a header file which corresponds to this and the class “KeyboardExercise” has this header file:

class KeyboardExercise : public Component,
public ButtonListener,
public SliderListener,
public AudioIODeviceCallback,
public KeyListener,
public ChangeListener,
public FilenameComponentListener
{
public:
KeyboardExercise();
~KeyboardExercise();

void audioDeviceIOCallback (const float** inputChannelData,
int totalNumInputChannels,
float** outputChannelData,
int totalNumOutputChannels,
int numSamples);
void audioDeviceAboutToStart (AudioIODevice* device);
void audioDeviceStopped();
void paint(Graphics& g);
void resized();
void updateButtons();
void buttonClicked (Button* button);
void sliderValueChanged (Slider* slider);
bool keyPressed(const KeyPress &key, Component* originatingComponent);
void filenameComponentChanged(FilenameComponent*);
void changeListenerCallback(void*);



private:
File currentFile;

Slider *tempoSlider, *volumeSlider;
TextButton *startButton, *metroButton;
AudioFormatReaderSource* currentAudioFileSource;
AudioSourcePlayer audioSourcePlayer;
Label *metroLabel, *volumeLabel;
FilenameComponent *fileChooser;
AudioFormatManager formatManager;
AudioTransportSource transportSource;
AudioFormatReaderSource *currentSource;
TextEditor* text;

int metroCount;
float tempo;
bool metroFlag;
AudioDeviceManager audioDeviceManager;

};

and this implementation:

[code]KeyboardExercise::KeyboardExercise()
{
const float defaultVolume = 0.8;
tempo = 22050;
metroCount = 0;
metroFlag = false;

formatManager.registerBasicFormats();
currentSource = 0;

addAndMakeVisible (fileChooser = new FilenameComponent (T(“audiofile”),
File::nonexistent,
true, false, false,
formatManager.getWildcardForAllFormats(),
String::empty,
T("(choose an exercise)")));
fileChooser->addListener (this);
fileChooser->setBrowseButtonText (T(“browse”));

addAndMakeVisible(startButton = new TextButton(T(“Start”)));
startButton->addButtonListener(this);

addAndMakeVisible(volumeSlider = new Slider (T(“volume”)));
volumeSlider->setSliderStyle(Slider::LinearVertical);
volumeSlider->setTextBoxStyle(Slider::TextBoxBelow, false, 50, 20);
volumeSlider->setRange(0,1,0.1);
volumeSlider->setValue(defaultVolume);
addAndMakeVisible(volumeLabel = new Label (T(“volumeLabel”),T(“VOLUME”)));

addAndMakeVisible(metroButton = new TextButton (T(“on/off”)));
metroButton->setClickingTogglesState(true);
addAndMakeVisible(metroLabel = new Label (T(“metroLabel”),T(“METRONOME”)));

addAndMakeVisible(text = new TextEditor (T(“text”)));

const String error (audioDeviceManager.initialise (1, /* number of input channels /
2, /
number of output channels /
0, /
no XML settings… /
true /
select default device on failure */));

if (error.isNotEmpty())
{
AlertWindow::showMessageBox (AlertWindow::WarningIcon,
T(“MainComponent”),
T(“Couldn’t open an output device!\n\n”) + error);
}
else
{
// start the IO device pulling its data from our callback…
audioDeviceManager.setAudioCallback (this);
audioSourcePlayer.setSource(&transportSource);

}

}

KeyboardExercise::~KeyboardExercise()
{
audioSourcePlayer.setSource(0);
audioDeviceManager.setAudioCallback (0);
deleteAllChildren();
}

void KeyboardExercise::audioDeviceIOCallback (const float** inputChannelData,
int totalNumInputChannels,
float** outputChannelData,
int totalNumOutputChannels,
int numSamples)
{
float gain = volumeSlider->getValue();
int window = tempo/4;

const float *in = inputChannelData[0];
float *out = outputChannelData[0];

audioSourcePlayer.audioDeviceIOCallback (inputChannelData, totalNumInputChannels, outputChannelData, totalNumOutputChannels, numSamples);

while(–numSamples)
{
if (metroFlag==true)
{

if (metroCount>tempo)
{
*out = 0.0;
*out = gain;
metroCount = 0;
}

metroCount++;
}
}
}

void KeyboardExercise::audioDeviceAboutToStart (AudioIODevice* device)
{
audioSourcePlayer.audioDeviceAboutToStart (device);
}
void KeyboardExercise::audioDeviceStopped()
{
audioSourcePlayer.audioDeviceStopped();
}

void KeyboardExercise::paint (Graphics& g)
{
g.setColour(Colours::darkgrey);
g.setOpacity(0.6);
g.fillRect(0,getHeight()/1.7,70,getHeight());
g.fillRect(0,getHeight()/2.625,70,getHeight()/4.7);
g.drawRect(getWidth()/3, getHeight()-50, getWidth()/3, 30);
g.drawFittedText(T(“SPACEBAR”),getWidth()/3,getHeight()-50,getWidth()/3,30,Justification::centred,1);
}

void KeyboardExercise::resized()
{
//metroButton->setBounds (getWidth()/5.5, getHeight()/1.235, getWidth()/7, getHeight()/7);
volumeSlider->setBounds (7, getHeight()/1.5, 50, getHeight()/3.5);
volumeLabel->setBounds (5, getHeight()/1.7, 60, 70);

metroButton->setBounds (10, getHeight()/2.2, 45, 45);
metroLabel->setBounds (5, getHeight()/2.7, 60, 70);
startButton->setBounds (10, getHeight()/3.5, 50, 30);
fileChooser->setBounds (10, 10, getWidth() - 20, 24);
text->setBounds (10, 50, getWidth()-20, 80);
updateButtons();
}

void KeyboardExercise::updateButtons()
{
repaint();
}

void KeyboardExercise::buttonClicked (Button* button)
{
if (button == startButton)
{
transportSource.setPosition (0.0);
transportSource.start();
}

if (metroButton->getToggleState())
metroFlag = true;
}

void KeyboardExercise::sliderValueChanged (Slider* slider)
{
}

bool KeyboardExercise::keyPressed(const KeyPress &key, Component* originatingComponent)
{
return true;
}

void KeyboardExercise::filenameComponentChanged(FilenameComponent*)
{
File audioFile (fileChooser->getCurrentFile());

transportSource.stop();
transportSource.setSource (0);
deleteAndZero (currentSource);

AudioFormatManager formatManager;
formatManager.registerBasicFormats();

AudioFormatReader* reader = formatManager.createReaderFor (audioFile);

if (reader != 0)
{
currentFile = audioFile;
currentSource = new AudioFormatReaderSource (reader, true);
transportSource.setSource (currentSource, 0, reader->sampleRate);
}

updateButtons();
}

void KeyboardExercise::changeListenerCallback(void*)
{
updateButtons();
}[/code]

The program compiles but for some reason it crashes immediately. When I comment out the following line the tabbed component loads but obviously without anything in it:

addTab (T("Keys"), Colours::orchid, new KeyboardExercise(), true);
It must have a problem instantiating KeyboardExercise but I dont know why because it worked yesterday. Any ideas?


#2

sorry, doesn’t crash here when pasted into the Juce demo…!