MCPcopy Create free account
hub / github.com/OpenShot/libopenshot / ResampleMappedAudio

Method ResampleMappedAudio

src/FrameMapper.cpp:878–1094  ·  view source on GitHub ↗

Resample audio and map channels (if needed)

Source from the content-addressed store, hash-verified

876
877// Resample audio and map channels (if needed)
878void FrameMapper::ResampleMappedAudio(std::shared_ptr<Frame> frame, int64_t original_frame_number)
879{
880 // Check if mappings are dirty (and need to be recalculated)
881 if (is_dirty)
882 // Recalculate mappings
883 Init();
884
885 // Init audio buffers / variables
886 int total_frame_samples = 0;
887 int channels_in_frame = frame->GetAudioChannelsCount();
888 int sample_rate_in_frame = frame->SampleRate();
889 int samples_in_frame = frame->GetAudioSamplesCount();
890 ChannelLayout channel_layout_in_frame = frame->ChannelsLayout();
891
892 ZmqLogger::Instance()->AppendDebugMethod(
893 "FrameMapper::ResampleMappedAudio",
894 "frame->number", frame->number,
895 "original_frame_number", original_frame_number,
896 "channels_in_frame", channels_in_frame,
897 "samples_in_frame", samples_in_frame,
898 "sample_rate_in_frame", sample_rate_in_frame);
899
900 // Get audio sample array
901 float* frame_samples_float = NULL;
902 // Get samples interleaved together (c1 c2 c1 c2 c1 c2)
903 frame_samples_float = frame->GetInterleavedAudioSamples(&samples_in_frame);
904
905 // Calculate total samples
906 total_frame_samples = samples_in_frame * channels_in_frame;
907
908 // Create a new array (to hold all S16 audio samples for the current queued frames)
909 int16_t* frame_samples = (int16_t*) av_malloc(sizeof(int16_t)*total_frame_samples);
910
911 // Translate audio sample values back to 16 bit integers with saturation
912 float valF;
913 int16_t conv;
914 const int16_t max16 = 32767;
915 const int16_t min16 = -32768;
916 for (int s = 0; s < total_frame_samples; s++) {
917 valF = frame_samples_float[s] * (1 << 15);
918 if (valF > max16)
919 conv = max16;
920 else if (valF < min16)
921 conv = min16;
922 else
923 conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
924
925 // Copy into buffer
926 frame_samples[s] = conv;
927 }
928
929 // Deallocate float array
930 delete[] frame_samples_float;
931 frame_samples_float = NULL;
932
933 // Create input frame (and allocate arrays)
934 AVFrame *audio_frame = AV_ALLOCATE_FRAME();
935 AV_RESET_FRAME(audio_frame);

Callers

nothing calls this directly

Calls 11

av_err2stringFunction · 0.85
ErrorEncodingVideoClass · 0.85
GetAudioChannelsCountMethod · 0.80
GetAudioSamplesCountMethod · 0.80
AppendDebugMethodMethod · 0.80
ResizeAudioMethod · 0.80
AddAudioMethod · 0.80
SampleRateMethod · 0.45
ChannelsLayoutMethod · 0.45

Tested by

no test coverage detected