Added a DisplayWaveform method on a frame, to help debug audio issues. It uses ImageMagick to draw each channel and all samples in each channel.

This commit is contained in:
Jonathan Thomas
2011-10-24 17:32:26 -05:00
parent 43fd6c33d0
commit 81941041e3
4 changed files with 88 additions and 5 deletions

View File

@@ -121,6 +121,77 @@ void Frame::Display()
image->display();
}
// Display the wave form
void Frame::DisplayWaveform(bool resize)
{
// Create black image
int width = audio->getNumSamples();
int height = 200 * audio->getNumChannels();
int height_padding = 20 * (audio->getNumChannels() - 1);
int total_height = height + height_padding;
Magick::Image wave_image(Magick::Geometry(width, total_height), Magick::Color("#000000"));
// Init a list of lines
list<Magick::Drawable> lines;
lines.push_back(Magick::DrawableFillColor("#0070ff"));
lines.push_back(Magick::DrawablePointSize(16));
// Loop through each audio channel
int Y = 100;
for (int channel = 0; channel < audio->getNumChannels(); channel++)
{
// Get audio for this channel
float *samples = audio->getSampleData(channel);
for (int sample = 0; sample < audio->getNumSamples(); sample++)
{
// Sample value (scaled to -100 to 100)
float value = samples[sample] * 100;
if (value > 100 || value < -100)
cout << "TOO BIG!!! " << value << endl;
// Append a line segment for each sample
if (value != 0.0)
{
// LINE
lines.push_back(Magick::DrawableStrokeColor("#0070ff"));
lines.push_back(Magick::DrawableStrokeWidth(1));
lines.push_back(Magick::DrawableLine(sample,Y, sample,Y-value)); // sample=X coordinate, Y=100 is the middle
}
else
{
// DOT
lines.push_back(Magick::DrawableFillColor("#0070ff"));
lines.push_back(Magick::DrawableStrokeWidth(1));
lines.push_back(Magick::DrawablePoint(sample,Y));
}
}
// Add Channel Label
stringstream label;
label << "Channel " << channel;
lines.push_back(Magick::DrawableStrokeColor("#ffffff"));
lines.push_back(Magick::DrawableFillColor("#ffffff"));
lines.push_back(Magick::DrawableStrokeWidth(0.1));
lines.push_back(Magick::DrawableText(5, Y - 5, label.str()));
// Increment Y
Y += (200 + height_padding);
}
// Draw the waveform
wave_image.draw(lines);
// Resize Image (if requested)
if (resize)
// Resize to 60%
wave_image.resize(Magick::Geometry(width * 0.6, total_height * 0.6));
// Display Image
wave_image.display();
}
// Get pixel data (as packets)
const Magick::PixelPacket* Frame::GetPixels()
{