Support frequency resampling for wav audio
This commit is contained in:
parent
56505ebe85
commit
104d4a92a0
3 changed files with 19 additions and 3 deletions
|
|
@ -26,6 +26,8 @@ namespace psemek::audio
|
||||||
return result_;
|
return result_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<float> grab_result();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<float> result_;
|
std::vector<float> result_;
|
||||||
int position_{0};
|
int position_{0};
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,15 @@ namespace psemek::audio
|
||||||
last_sample_[1] = samples[samples.size() - 1];
|
last_sample_[1] = samples[samples.size() - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<float> resampler::grab_result()
|
||||||
|
{
|
||||||
|
position_ = 0;
|
||||||
|
position_frac_ = 0.f;
|
||||||
|
last_sample_[0] = 0.f;
|
||||||
|
last_sample_[1] = 0.f;
|
||||||
|
return std::move(result_);
|
||||||
|
}
|
||||||
|
|
||||||
void resampler::advance(float delta)
|
void resampler::advance(float delta)
|
||||||
{
|
{
|
||||||
position_frac_ += delta;
|
position_frac_ += delta;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <psemek/audio/track.hpp>
|
#include <psemek/audio/track.hpp>
|
||||||
#include <psemek/audio/constants.hpp>
|
#include <psemek/audio/constants.hpp>
|
||||||
|
#include <psemek/audio/detail/resampler.hpp>
|
||||||
#include <psemek/util/to_string.hpp>
|
#include <psemek/util/to_string.hpp>
|
||||||
#include <psemek/util/at_scope_exit.hpp>
|
#include <psemek/util/at_scope_exit.hpp>
|
||||||
#include <psemek/sdl2/init.hpp>
|
#include <psemek/sdl2/init.hpp>
|
||||||
|
|
@ -17,9 +18,6 @@ namespace psemek::audio
|
||||||
if (spec.channels > 2)
|
if (spec.channels > 2)
|
||||||
throw std::runtime_error(util::to_string("Can't convert audio with ", static_cast<int>(spec.channels), " channels"));
|
throw std::runtime_error(util::to_string("Can't convert audio with ", static_cast<int>(spec.channels), " channels"));
|
||||||
|
|
||||||
if (spec.freq != audio::frequency)
|
|
||||||
throw std::runtime_error(util::to_string("Can't convert audio with frequency ", spec.freq));
|
|
||||||
|
|
||||||
if (spec.format != AUDIO_S16SYS)
|
if (spec.format != AUDIO_S16SYS)
|
||||||
throw std::runtime_error(util::to_string("Can't convert audio with format ", spec.format));
|
throw std::runtime_error(util::to_string("Can't convert audio with format ", spec.format));
|
||||||
|
|
||||||
|
|
@ -44,6 +42,13 @@ namespace psemek::audio
|
||||||
result[i] = (p[i] * 2.f + 1.f) / 65536.f;
|
result[i] = (p[i] * 2.f + 1.f) / 65536.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (spec.freq != audio::frequency)
|
||||||
|
{
|
||||||
|
audio::resampler resampler(audio::frequency * 1.f / spec.freq);
|
||||||
|
resampler.feed(result);
|
||||||
|
result = resampler.grab_result();
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue