|
| 1 | +/* |
| 2 | + * This file is part of OpenTTD. |
| 3 | + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. |
| 4 | + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 5 | + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. |
| 6 | + */ |
| 7 | + |
| 8 | +/** @file soundresampler_soxr.cpp SOXR sound resampler. */ |
| 9 | + |
| 10 | +#include "stdafx.h" |
| 11 | +#include "core/math_func.hpp" |
| 12 | +#include "debug.h" |
| 13 | +#include "sound_type.h" |
| 14 | +#include "soundresampler_type.h" |
| 15 | + |
| 16 | +#include <soxr.h> |
| 17 | +#include <thread> |
| 18 | + |
| 19 | +#include "safeguards.h" |
| 20 | + |
| 21 | +class SoundResampler_Soxr : public SoundResampler { |
| 22 | +public: |
| 23 | + SoundResampler_Soxr() : SoundResampler("soxr", "SOXR sound resampler", 0) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Convert samples from 8 bits to 16 bits. |
| 27 | + * @param in Vector of samples to convert. |
| 28 | + * @param out Vector to place converted samples. |
| 29 | + * @pre out vector must be exactly twice the size of in vector. |
| 30 | + */ |
| 31 | + static void ConvertInt8toInt16(std::vector<std::byte> &in, std::vector<std::byte> &out) |
| 32 | + { |
| 33 | + assert(std::size(out) == std::size(in) * 2); |
| 34 | + |
| 35 | + auto out_it = std::begin(out); |
| 36 | + for (const std::byte &value : in) { |
| 37 | + if constexpr (std::endian::native != std::endian::little) { |
| 38 | + *out_it++ = value; |
| 39 | + *out_it++ = std::byte{0}; |
| 40 | + } else { |
| 41 | + *out_it++ = std::byte{0}; |
| 42 | + *out_it++ = value; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + bool Resample(SoundEntry &sound, uint32_t play_rate) override |
| 48 | + { |
| 49 | + /* The sound data to work on. */ |
| 50 | + std::vector<std::byte> &data = *sound.data; |
| 51 | + |
| 52 | + std::vector<std::byte> tmp; |
| 53 | + if (sound.bits_per_sample == 16) { |
| 54 | + /* No conversion necessary so just move from sound data to temporary buffer. */ |
| 55 | + data.swap(tmp); |
| 56 | + } else { |
| 57 | + /* SoxR cannot resample 8-bit audio, so convert from 8-bit to 16-bit into temporary buffer. */ |
| 58 | + tmp.resize(std::size(data) * sizeof(int16_t)); |
| 59 | + ConvertInt8toInt16(data, tmp); |
| 60 | + sound.bits_per_sample = 16; |
| 61 | + } |
| 62 | + |
| 63 | + /* Resize buffer ensuring it is correctly aligned. */ |
| 64 | + uint align = sound.channels * sound.bits_per_sample / 8; |
| 65 | + data.resize(Align(std::size(tmp) * play_rate / sound.rate, align)); |
| 66 | + |
| 67 | + soxr_error_t error = soxr_oneshot(sound.rate, play_rate, sound.channels, |
| 68 | + std::data(tmp), std::size(tmp) / align, nullptr, |
| 69 | + std::data(data), std::size(data) / align, nullptr, |
| 70 | + &this->io, &this->quality, &this->runtime); |
| 71 | + |
| 72 | + if (error != nullptr) { |
| 73 | + /* Could not resample, try using the original data as-is without resampling instead. */ |
| 74 | + Debug(misc, 0, "Failed to resample: {}", soxr_strerror(error)); |
| 75 | + data.swap(tmp); |
| 76 | + } else { |
| 77 | + sound.rate = play_rate; |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + static SoundResampler_Soxr instance; |
| 85 | + |
| 86 | + const soxr_io_spec_t io = soxr_io_spec(SOXR_INT16_I, SOXR_INT16_I); // 16-bit input and output. |
| 87 | + const soxr_quality_spec_t quality = soxr_quality_spec(SOXR_VHQ, 0); // Use 'Very high quality'. |
| 88 | + const soxr_runtime_spec_t runtime = soxr_runtime_spec(std::thread::hardware_concurrency()); // Enable multi-threading. |
| 89 | +}; |
| 90 | + |
| 91 | +/* static */ SoundResampler_Soxr SoundResampler_Soxr::instance{}; |
0 commit comments