|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Spotify AB. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | +#include "DecoderSpeexImplementation.h" |
| 22 | + |
| 23 | +#include <cstdlib> |
| 24 | +#include <future> |
| 25 | + |
| 26 | +#include <speex/speex_header.h> |
| 27 | + |
| 28 | +namespace nativeformat { |
| 29 | +namespace decoder { |
| 30 | + |
| 31 | +DecoderSpeexImplementation::DecoderSpeexImplementation(std::shared_ptr<DataProvider> &data_provider) |
| 32 | + : _data_provider(data_provider), |
| 33 | + _state(nullptr), |
| 34 | + _channels(1), |
| 35 | + _samplerate(0.0), |
| 36 | + _frames(0), |
| 37 | + _frame_index(0), |
| 38 | + _current_section(0) {} |
| 39 | + |
| 40 | +DecoderSpeexImplementation::~DecoderSpeexImplementation() { |
| 41 | + if (_state != nullptr) { |
| 42 | + speex_decoder_destroy(_state); |
| 43 | + speex_bits_destroy(&_bits); |
| 44 | + _state = nullptr; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const std::string &DecoderSpeexImplementation::name() { |
| 49 | + static const std::string domain("com.nativeformat.decoder.speex"); |
| 50 | + return domain; |
| 51 | +} |
| 52 | + |
| 53 | +void DecoderSpeexImplementation::load(const ERROR_DECODER_CALLBACK &decoder_error_callback, |
| 54 | + const LOAD_DECODER_CALLBACK &decoder_load_callback) { |
| 55 | + std::shared_ptr<DecoderSpeexImplementation> strong_this = shared_from_this(); |
| 56 | + _load_future = std::async( |
| 57 | + std::launch::async, [strong_this, decoder_error_callback, decoder_load_callback]() { |
| 58 | + { |
| 59 | + std::lock_guard<std::mutex> speex_lock(strong_this->_speex_mutex); |
| 60 | + strong_this->_state = speex_decoder_init(&speex_nb_mode); |
| 61 | + int tmp = 1; |
| 62 | + speex_decoder_ctl(strong_this->_state, SPEEX_SET_ENH, &tmp); |
| 63 | + speex_bits_init(&strong_this->_bits); |
| 64 | + SpeexHeader header; |
| 65 | + strong_this->_data_provider->read(&header, sizeof(SpeexHeader), 1); |
| 66 | + strong_this->_samplerate = header.rate; |
| 67 | + strong_this->_channels = header.nb_channels; |
| 68 | + } |
| 69 | + decoder_load_callback(true); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +double DecoderSpeexImplementation::sampleRate() { |
| 74 | + return _samplerate; |
| 75 | +} |
| 76 | + |
| 77 | +int DecoderSpeexImplementation::channels() { |
| 78 | + return _channels; |
| 79 | +} |
| 80 | + |
| 81 | +long DecoderSpeexImplementation::currentFrameIndex() { |
| 82 | + return _frame_index; |
| 83 | +} |
| 84 | + |
| 85 | +void DecoderSpeexImplementation::seek(long frame_index) { |
| 86 | + flush(); |
| 87 | + { |
| 88 | + std::lock_guard<std::mutex> speex_lock(_speex_mutex); |
| 89 | + _data_provider->seek(sizeof(SpeexHeader), SEEK_SET); |
| 90 | + int frame_size = 0; |
| 91 | + speex_decoder_ctl(_state, SPEEX_GET_FRAME_SIZE, &frame_size); |
| 92 | + long current_frame_index = 0; |
| 93 | + while (!_data_provider->eof() && current_frame_index < frame_index) { |
| 94 | + _cached_samples.clear(); |
| 95 | + char read_bytes[256]; |
| 96 | + const auto bytes_read = _data_provider->read(read_bytes, sizeof(char), sizeof(read_bytes)); |
| 97 | + speex_bits_read_from(&_bits, read_bytes, bytes_read); |
| 98 | + float samples[frame_size]; |
| 99 | + const auto samples_read = speex_decode(_state, &_bits, samples); |
| 100 | + _cached_samples.insert(_cached_samples.begin(), samples, samples + samples_read); |
| 101 | + current_frame_index += samples_read; |
| 102 | + } |
| 103 | + _frame_index = frame_index; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +long DecoderSpeexImplementation::frames() { |
| 108 | + return UNKNOWN_FRAMES; |
| 109 | +} |
| 110 | + |
| 111 | +void DecoderSpeexImplementation::decode(long frames, |
| 112 | + const DECODE_CALLBACK &decode_callback, |
| 113 | + bool synchronous) { |
| 114 | + std::shared_ptr<DecoderSpeexImplementation> strong_this = shared_from_this(); |
| 115 | + auto run_thread = [strong_this, decode_callback, frames] { |
| 116 | + long frame_index = strong_this->currentFrameIndex(); |
| 117 | + float *samples = (float *)malloc(frames * sizeof(float) * strong_this->_channels); |
| 118 | + long read_frames = 0; |
| 119 | + { |
| 120 | + std::lock_guard<std::mutex> speex_lock(strong_this->_speex_mutex); |
| 121 | + int frame_size = 0; |
| 122 | + speex_decoder_ctl(strong_this->_state, SPEEX_GET_FRAME_SIZE, &frame_size); |
| 123 | + while (!strong_this->_data_provider->eof() && strong_this->_cached_samples.size() < frames) { |
| 124 | + char read_bytes[256]; |
| 125 | + const auto bytes_read = strong_this->_data_provider->read(read_bytes, sizeof(char), sizeof(read_bytes)); |
| 126 | + speex_bits_read_from(&strong_this->_bits, read_bytes, bytes_read); |
| 127 | + float samples[frame_size]; |
| 128 | + const auto samples_read = speex_decode(strong_this->_state, &strong_this->_bits, samples); |
| 129 | + strong_this->_cached_samples.insert(strong_this->_cached_samples.end(), samples, samples + samples_read); |
| 130 | + } |
| 131 | + } |
| 132 | + read_frames = std::min(static_cast<long>(strong_this->_cached_samples.size()), frames); |
| 133 | + memcpy(samples, strong_this->_cached_samples.data(), read_frames * sizeof(float)); |
| 134 | + strong_this->_cached_samples.erase(strong_this->_cached_samples.begin(), strong_this->_cached_samples.begin() + read_frames); |
| 135 | + decode_callback(frame_index, read_frames, samples); |
| 136 | + free(samples); |
| 137 | + }; |
| 138 | + if (synchronous) { |
| 139 | + run_thread(); |
| 140 | + } else { |
| 141 | + std::thread(run_thread).detach(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +bool DecoderSpeexImplementation::eof() { |
| 146 | + return _data_provider->eof(); |
| 147 | +} |
| 148 | + |
| 149 | +const std::string &DecoderSpeexImplementation::path() { |
| 150 | + return _data_provider->path(); |
| 151 | +} |
| 152 | + |
| 153 | +void DecoderSpeexImplementation::flush() { |
| 154 | + std::lock_guard<std::mutex> speex_lock(_speex_mutex); |
| 155 | + speex_decoder_ctl(_state, SPEEX_RESET_STATE, nullptr); |
| 156 | + speex_bits_reset(&_bits); |
| 157 | + _cached_samples.clear(); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace decoder |
| 161 | +} // namespace nativeformat |
0 commit comments