Fixed bug on Image processing, with no video or audio present. Play() and DisplayWaveform() methods now handle not having any audio samples.

This commit is contained in:
Jonathan Thomas
2011-10-27 11:27:44 -05:00
parent 28b9fbabd1
commit bfc05d507d
3 changed files with 77 additions and 50 deletions

View File

@@ -760,9 +760,9 @@ Frame FFmpegReader::CreateFrame(int requested_frame)
void FFmpegReader::CheckWorkingFrames(bool end_of_stream)
{
// Adjust for video only, or audio only
if (info.video_stream_index < 0)
if (!info.has_video)
last_video_frame = last_audio_frame;
if (info.audio_stream_index < 0)
if (!info.has_audio)
last_audio_frame = last_video_frame;
// Loop through all working queue frames

View File

@@ -124,70 +124,92 @@ void Frame::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"));
// Create blank image
Magick::Image wave_image;
// 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++)
// Calculate the width of an image based on the # of samples
int width = audio->getNumSamples();
if (width > 0)
{
// Get audio for this channel
float *samples = audio->getSampleData(channel);
// If samples are present...
int height = 200 * audio->getNumChannels();
int height_padding = 20 * (audio->getNumChannels() - 1);
int total_height = height + height_padding;
wave_image = Magick::Image(Magick::Geometry(width, total_height), Magick::Color("#000000"));
for (int sample = 0; sample < audio->getNumSamples(); sample++)
// Loop through each audio channel
int Y = 100;
for (int channel = 0; channel < audio->getNumChannels(); channel++)
{
// Sample value (scaled to -100 to 100)
float value = samples[sample] * 100;
// Get audio for this channel
float *samples = audio->getSampleData(channel);
if (value > 100 || value < -100)
cout << "TOO BIG!!! " << value << endl;
for (int sample = 0; sample < audio->getNumSamples(); sample++)
{
// Sample value (scaled to -100 to 100)
float value = samples[sample] * 100;
// 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));
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));
}
else
{
// No audio samples present
wave_image = Magick::Image(Magick::Geometry(720, 480), Magick::Color("#000000"));
// 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()));
lines.push_back(Magick::DrawableText(265, 240, "No Audio Samples Found"));
// Increment Y
Y += (200 + height_padding);
// Draw the waveform
wave_image.draw(lines);
}
// 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();
}
@@ -268,6 +290,10 @@ void Frame::AddAudio(int destChannel, int destStartSample, const float* source,
// Play audio samples for this frame
void Frame::Play()
{
// Check if samples are present
if (!audio->getNumSamples())
return;
AudioDeviceManager deviceManager;
deviceManager.initialise (0, /* number of input channels */
audio->getNumChannels(), /* number of output channels */

View File

@@ -18,20 +18,21 @@ int main()
// openshot::FFmpegReader r("/home/jonathan/Aptana Studio Workspace/OpenShotLibrary/src/examples/test1.mp4");
// openshot::FFmpegReader r("/home/jonathan/Videos/OpenShot_Now_In_3d.mp4");
// openshot::FFmpegReader r("/home/jonathan/Videos/sintel-1024-stereo.mp4");
// openshot::FFmpegReader r("/home/jonathan/Videos/sintel_trailer-720p.mp4");
openshot::FFmpegReader r("/home/jonathan/Videos/sintel_trailer-720p.mp4");
// openshot::FFmpegReader r("/home/jonathan/Music/beat.wav");
openshot::FFmpegReader r("/home/jonathan/Music/lonely island/B004YRCAOO_(disc_1)_09_-_Shy_Ronnie_2__Ronnie_&_Clyde_[Expli.mp3");
// openshot::FFmpegReader r("/home/jonathan/Documents/OpenShot Art/bannertemplate.png");
// openshot::FFmpegReader r("/home/jonathan/Aptana Studio Workspace/OpenShotLibrary/src/examples/CMakeLists.txt");
// openshot::FFmpegReader r("/home/jonathan/Music/lonely island/B004YRCAOO_(disc_1)_09_-_Shy_Ronnie_2__Ronnie_&_Clyde_[Expli.mp3");
// openshot::FFmpegReader r("/home/jonathan/Pictures/Photo 3 - Sky.jpeg");
// openshot::FFmpegReader r("/home/jonathan/Videos/60fps.mp4");
// openshot::FFmpegReader r("/home/jonathan/Aptana Studio Workspace/OpenShotLibrary/src/examples/asdf.wdf");
// Display debug info
r.DisplayInfo();
for (int frame = 990; frame < 2000; frame++)
for (int frame = 1000; frame < 2000; frame++)
{
Frame f = r.GetFrame(frame);
f.Play();
f.Display();
f.DisplayWaveform(false);
}