You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-03-02 08:53:52 -08:00
Merge pull request #466 from SuslikV/float-to-int16
Fix distortion during mix of audio
This commit is contained in:
@@ -1627,9 +1627,8 @@ void FFmpegReader::ProcessAudioPacket(int64_t requested_frame, int64_t target_fr
|
||||
else
|
||||
partial_frame = true;
|
||||
|
||||
// Add samples for current channel to the frame. Reduce the volume to 98%, to prevent
|
||||
// some louder samples from maxing out at 1.0 (not sure why this happens)
|
||||
f->AddAudio(true, channel_filter, start, iterate_channel_buffer, samples, 0.98f);
|
||||
// Add samples for current channel to the frame.
|
||||
f->AddAudio(true, channel_filter, start, iterate_channel_buffer, samples, 1.0f);
|
||||
|
||||
// Debug output
|
||||
ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::ProcessAudioPacket (f->AddAudio)", "frame", starting_frame_number, "start", start, "samples", samples, "channel", channel_filter, "partial_frame", partial_frame, "samples_per_frame", samples_per_frame);
|
||||
|
||||
@@ -1525,11 +1525,23 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
|
||||
// Calculate total samples
|
||||
total_frame_samples = samples_in_frame * channels_in_frame;
|
||||
|
||||
// Translate audio sample values back to 16 bit integers
|
||||
for (int s = 0; s < total_frame_samples; s++, frame_position++)
|
||||
// Translate sample value and copy into buffer
|
||||
all_queued_samples[frame_position] = int(frame_samples_float[s] * (1 << 15));
|
||||
// Translate audio sample values back to 16 bit integers with saturation
|
||||
float valF;
|
||||
int16_t conv;
|
||||
const int16_t max16 = 32767;
|
||||
const int16_t min16 = -32768;
|
||||
for (int s = 0; s < total_frame_samples; s++, frame_position++) {
|
||||
valF = frame_samples_float[s] * (1 << 15);
|
||||
if (valF > max16)
|
||||
conv = max16;
|
||||
else if (valF < min16)
|
||||
conv = min16;
|
||||
else
|
||||
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
|
||||
|
||||
// Copy into buffer
|
||||
all_queued_samples[frame_position] = conv;
|
||||
}
|
||||
|
||||
// Deallocate float array
|
||||
delete[] frame_samples_float;
|
||||
|
||||
@@ -785,10 +785,23 @@ void FrameMapper::ResampleMappedAudio(std::shared_ptr<Frame> frame, int64_t orig
|
||||
// Create a new array (to hold all S16 audio samples for the current queued frames)
|
||||
int16_t* frame_samples = (int16_t*) av_malloc(sizeof(int16_t)*total_frame_samples);
|
||||
|
||||
// Translate audio sample values back to 16 bit integers
|
||||
for (int s = 0; s < total_frame_samples; s++)
|
||||
// Translate sample value and copy into buffer
|
||||
frame_samples[s] = int(frame_samples_float[s] * (1 << 15));
|
||||
// Translate audio sample values back to 16 bit integers with saturation
|
||||
float valF;
|
||||
int16_t conv;
|
||||
const int16_t max16 = 32767;
|
||||
const int16_t min16 = -32768;
|
||||
for (int s = 0; s < total_frame_samples; s++) {
|
||||
valF = frame_samples_float[s] * (1 << 15);
|
||||
if (valF > max16)
|
||||
conv = max16;
|
||||
else if (valF < min16)
|
||||
conv = min16;
|
||||
else
|
||||
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
|
||||
|
||||
// Copy into buffer
|
||||
frame_samples[s] = conv;
|
||||
}
|
||||
|
||||
|
||||
// Deallocate float array
|
||||
|
||||
@@ -76,8 +76,8 @@ TEST(FFmpegReader_Check_Audio_File)
|
||||
CHECK_CLOSE(0.0f, samples[50], 0.00001);
|
||||
CHECK_CLOSE(0.0f, samples[100], 0.00001);
|
||||
CHECK_CLOSE(0.0f, samples[200], 0.00001);
|
||||
CHECK_CLOSE(0.160781f, samples[230], 0.00001);
|
||||
CHECK_CLOSE(-0.06125f, samples[300], 0.00001);
|
||||
CHECK_CLOSE(0.16406f, samples[230], 0.00001);
|
||||
CHECK_CLOSE(-0.06250f, samples[300], 0.00001);
|
||||
|
||||
// Close reader
|
||||
r.Close();
|
||||
|
||||
Reference in New Issue
Block a user