Skip to content

Commit

Permalink
Merge pull request #466 from SuslikV/float-to-int16
Browse files Browse the repository at this point in the history
Fix distortion during mix of audio
  • Loading branch information
ferdnyc committed Mar 26, 2020
2 parents fda1357 + 455b6e9 commit c38c55f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/FFmpegReader.cpp
Expand Up @@ -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);
Expand Down
22 changes: 17 additions & 5 deletions src/FFmpegWriter.cpp
Expand Up @@ -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;
Expand Down
21 changes: 17 additions & 4 deletions src/FrameMapper.cpp
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/FFmpegReader_Tests.cpp
Expand Up @@ -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();
Expand Down

0 comments on commit c38c55f

Please sign in to comment.