diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 97239498..60a6ff32 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -524,17 +524,11 @@ void FFmpegWriter::write_audio_packet(Frame* frame) { // Translate sample value and copy into buffer frame_samples[s] = int(frame_samples_float[s] * (1 << 15)); - -// // DEBUG -// if (frame->number == 1) -// { -// cout << frame_samples[s] << endl; -// } - } - // Re-sample audio samples (if needed) - if (c->sample_fmt != AV_SAMPLE_FMT_S16 || info.channels != channels_in_frame || info.sample_rate != sample_rate_in_frame) { + // Re-sample audio samples (into additinal channels or changing the sample format / number format) + // The sample rate has already been resampled using the GetInterleavedAudioSamples method. + if (c->sample_fmt != AV_SAMPLE_FMT_S16 || info.channels != channels_in_frame) { // Audio needs to be converted // Create an audio resample context object (used to convert audio samples) @@ -550,14 +544,6 @@ void FFmpegWriter::write_audio_packet(Frame* frame) total_frame_samples = audio_resample(resampleCtx, (short *) converted_audio, (short *) frame_samples, total_frame_samples); remaining_frame_samples = total_frame_samples; - // DEBUG -// cout << endl << endl << endl << "END OF RESAMPLE DATA:" << endl; -// if (frame->number == 1) -// { -// for (int s = total_frame_samples - 10; s < (total_frame_samples + 200); s++) -// cout << converted_audio[s] << endl; -// } - // Copy audio samples over original samples memcpy(frame_samples, converted_audio, total_frame_samples * av_get_bytes_per_sample(c->sample_fmt)); @@ -566,14 +552,6 @@ void FFmpegWriter::write_audio_packet(Frame* frame) } } -// // DEBUG -// cout << endl << endl << endl << "AFTER RESCALE" << endl; -// if (frame->number == 1) -// { -// for (int s = 0; s < total_frame_samples; s++) -// cout << frame_samples[s] << endl; -// } - // Loop until no more samples while (remaining_frame_samples > 0) { // Get remaining samples needed for this packet @@ -586,13 +564,6 @@ void FFmpegWriter::write_audio_packet(Frame* frame) else if (remaining_frame_samples < remaining_packet_samples) diff = remaining_frame_samples; - - // DEBUG - cout << "Frame: " << frame->number << ", audio_input_position: " << audio_input_position << ", samples_position: " << samples_position << ", total_frame_samples: " << total_frame_samples << ", audio_input_frame_size: " << audio_input_frame_size << endl; - cout << " -- remaining_frame_samples: " << remaining_frame_samples << ", remaining_packet_samples: " << remaining_packet_samples << endl; - cout << " -- Wrote " << diff << endl; - - // Copy samples into input buffer (and convert to 16 bit int) for (int s = 0; s < diff; s++) { @@ -604,33 +575,13 @@ void FFmpegWriter::write_audio_packet(Frame* frame) samples_position++; remaining_frame_samples--; remaining_packet_samples--; - - // DEBUG -// if (frame->number == 1 && s > 2800) -// { -// cout << frame_samples[s] << endl; -// } -// if (frame->number == 2 && s < 100) -// { -// cout << frame_samples[s] << endl; -// } } - -// if (frame->number == 2 || frame->number == 3) -// { -// for (int z = 0; z < diff; z++) -// cout << frame_samples[z] << endl; -// } - - // Do we have enough samples to proceed? if (audio_input_position < audio_input_frame_size) // Not enough samples to encode... so wait until the next frame break; - cout << "NOW ENCODE FILE!" << endl; - // Set packet properties pkt.size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, (short *) samples); @@ -640,8 +591,6 @@ void FFmpegWriter::write_audio_packet(Frame* frame) pkt.stream_index = audio_st->index; pkt.data = audio_outbuf; - cout << "Final encode: frame " << frame->number << ", pkt.pts: " << pkt.pts << endl; - /* write the compressed frame in the media file */ if (av_interleaved_write_frame(oc, &pkt) != 0) throw ErrorEncodingAudio("Error while writing audio frame", frame->number); diff --git a/src/Frame.cpp b/src/Frame.cpp index c5ce5c05..eed3b809 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -255,30 +255,15 @@ float* Frame::GetInterleavedAudioSamples(int new_sample_rate, int* sample_count) { float *output = NULL; AudioSampleBuffer *buffer = audio; + AudioSampleBuffer *resampled_buffer = NULL; int num_of_channels = audio->getNumChannels(); int num_of_samples = audio->getNumSamples(); - - // DEBUG - cout << "output size 1: " << (num_of_channels * num_of_samples) << endl; - cout << "buffer channels 1: " << buffer->getNumChannels() << endl; - cout << "buffer samples 1: " << buffer->getNumSamples() << endl; - cout << "--------------" << endl; - -// // Loop through samples in each channel (combining them) +// DEBUG CODE // if (number == 1) -// { -// cout << "BEFORE RESAMPLE" << endl << endl; -// for (int sample = 0; sample < num_of_samples; sample++) -// { -// for (int channel = 0; channel < num_of_channels; channel++) -// { -// // Add sample to output array -// cout << buffer->getSampleData(channel)[sample] << endl; -// } -// } -// } - +// for (int s = 0; s < num_of_samples; s++) +// for (int c = 0; c < num_of_channels; c++) +// cout << buffer->getSampleData(c)[s] << endl; // Resample to new sample rate (if needed) if (new_sample_rate != sample_rate) @@ -295,35 +280,26 @@ float* Frame::GetInterleavedAudioSamples(int new_sample_rate, int* sample_count) double source_ratio = double(sample_rate) / double(new_sample_rate); double dest_ratio = double(new_sample_rate) / double(sample_rate); int new_num_of_samples = num_of_samples * dest_ratio; - resample_source.setResamplingRatio(dest_ratio); + resample_source.setResamplingRatio(source_ratio); // Prepare to resample resample_source.prepareToPlay(num_of_samples, new_sample_rate); // Create a buffer for the newly resampled data - AudioSampleBuffer resampled_buffer(num_of_channels, new_num_of_samples); - resampled_buffer.clear(); - const AudioSourceChannelInfo resample_callback_buffer = {&resampled_buffer, 0, new_num_of_samples}; + resampled_buffer = new AudioSampleBuffer(num_of_channels, new_num_of_samples); + const AudioSourceChannelInfo resample_callback_buffer = {resampled_buffer, 0, new_num_of_samples}; resample_callback_buffer.clearActiveBufferRegion(); // Resample the current frame's audio buffer (info the temp callback buffer) resample_source.getNextAudioBlock(resample_callback_buffer); // Update buffer pointer to this newly resampled buffer - buffer = &resampled_buffer; + buffer = resampled_buffer; // Update num_of_samples num_of_samples = new_num_of_samples; - } - - // DEBUG - cout << "output size 2: " << (num_of_channels * num_of_samples) << endl; - cout << "buffer channels 2: " << buffer->getNumChannels() << endl; - cout << "buffer samples 2: " << buffer->getNumSamples() << endl; - - // INTERLEAVE all samples together (channel 1 + channel 2 + channel 1 + channel 2, etc...) output = new float[num_of_channels * num_of_samples]; int position = 0; @@ -337,16 +313,14 @@ float* Frame::GetInterleavedAudioSamples(int new_sample_rate, int* sample_count) //cout << position << ", " << channel << ", " << sample << endl; output[position] = buffer->getSampleData(channel)[sample]; - if (number == 1) - { - cout << buffer->getSampleData(channel)[sample] << endl; - } - // increment position position++; } } + // Clean up + delete resampled_buffer; + // Update sample count (since it might have changed due to resampling) *sample_count = num_of_samples;