Waveform Drawing on a Thumbnail problem


#1

Hello,

I’m trying to figure out how to properly use the AudioThumbnail class and I don’t quite understand what is it that I’m missing. For now I just created a class called ‘AudioHandler’, in this class (following the Juce AudioPlayBack Demo) I don’t want to play the wav file just draw the waveform on the thumbnail. So basically what I have in my code so far is this class that just creates a Thumbnail to draw on and that I add to a mainWindow. In the main window class I have a button from which I load the file I want displayed on the thumbnail.
The code in the main window to load the file is :


void mainUi::loadFile()
{
	FileChooser fileChooser("Select Audio File to separate", File::getSpecialLocation(File::userHomeDirectory),"*.wav",true);
	
	if(fileChooser.browseForFileToOpen())
	{
		File wavFile(fileChooser.getResult());
		inputTextEditor->setText(wavFile.getFileName(),true);
		handler->showFile(wavFile);
	}
}

void mainUi::buttonClicked(Button *buttonThatWasClicked)
{
	if(buttonThatWasClicked == inputTextButton)
	{
		loadFile();
	}
}

And the class to create the Thumbnail and add it to the main UI as a component is:

class WaveThumbnail :	public Component,
						public ChangeListener
						//public FileDragAndDropTarget
{
	
public:
	
	WaveThumbnail() :  thumbnail(1024,formatManager,thumbnailCache), 
					   thumbnailCache(5)
					  
	{
		startTime = endTime = 0;
		formatManager.registerBasicFormats();
		thumbnail.addChangeListener(this);
	}
	
	~WaveThumbnail()
	{
		thumbnail.removeChangeListener(this);
	}
	
	void setFile(const File& file)
	{
		thumbnail.setSource(new FileInputSource(file));
		startTime = 0;
		endTime = thumbnail.getTotalLength();
	}
	
	void paint(Graphics& g)
	{
		g.fillAll(Colours::black);
		g.setColour(Colour(0xffc6d571));
		
		if(thumbnail.getTotalLength() > 0)
			thumbnail.drawChannels(g,getLocalBounds().reduced(3,3),startTime,endTime,0);
		else
		{
			g.setFont(12.0f);
			g.drawFittedText("No Audio File Selected",0,0,getWidth(),getHeight(),Justification::centred,2);
		}
		
	}
	
	void changeListenerCallback(ChangeBroadcaster*)
	{
		repaint();
	}
	
	/*bool isInterestedInFileDrag(const StringArray& )
	{
		return true;
	}*/

	AudioFormatManager formatManager;
	AudioThumbnail thumbnail;
	AudioThumbnailCache thumbnailCache;
	//AudioFormatReader formatReader;
	double startTime;
	double endTime;
};

AudioHandler::AudioHandler() : thumbnail(0),
							   playPauseButton(0)
{
	addAndMakeVisible(thumbnail = new WaveThumbnail()); 
	setSize(736,200);

}

AudioHandler::~AudioHandler()
{
	transportSource.setSource(0);
	//audioSourcePlayer.setSource(0);
	//deviceManager.removeAudioCallback(&audioSourcePlayer);
	
	deleteAllChildren();
}

void AudioHandler::paint(Graphics& g)
{
	g.fillAll(Colours::black);
}

void AudioHandler::resized()	
{
	thumbnail->setBounds(0,0,getWidth(),getHeight());
}

void AudioHandler::showFile(const File& file)
{
	loadFileIntoTransport(file);
	thumbnail->setFile(file);
}


void AudioHandler::loadFileIntoTransport(const File& audiofile)
{
	transportSource.stop();
	transportSource.setSource(0);
	currentAudioFileSource = 0;
	
	AudioFormatManager formatManager;
	formatManager.registerBasicFormats();
	
	AudioFormatReader *reader = formatManager.createReaderFor(audiofile);
	
	if(reader != 0)
	{
		currentAudioFileSource = new AudioFormatReaderSource(reader,true);
		transportSource.setSource(currentAudioFileSource,32768,reader->sampleRate);
	}
}

This places the thumbnail correctly, then I load the file and just two flat lines get drawn instead of the waveform of the wav file selected.
Could some please give me a hand into what am I doing wrong…

Thanks!


#2

nevermind just foudn my mistake!

was using the drawchannelfunction wrong.