|
| 1 | +#include <torchaudio/csrc/sox_effects_chain.h> |
| 2 | +#include <torchaudio/csrc/sox_utils.h> |
| 3 | + |
| 4 | +using namespace torch::indexing; |
| 5 | +using namespace torchaudio::sox_utils; |
| 6 | + |
| 7 | +namespace torchaudio { |
| 8 | +namespace sox_effects_chain { |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +// Helper struct to safely close sox_effect_t* pointer returned by |
| 13 | +// sox_create_effect |
| 14 | +struct SoxEffect { |
| 15 | + explicit SoxEffect(sox_effect_t* se) noexcept : se_(se){}; |
| 16 | + SoxEffect(const SoxEffect& other) = delete; |
| 17 | + SoxEffect(const SoxEffect&& other) = delete; |
| 18 | + SoxEffect& operator=(const SoxEffect& other) = delete; |
| 19 | + SoxEffect& operator=(SoxEffect&& other) = delete; |
| 20 | + ~SoxEffect() { |
| 21 | + if (se_ != nullptr) { |
| 22 | + free(se_); |
| 23 | + } |
| 24 | + } |
| 25 | + operator sox_effect_t*() const { |
| 26 | + return se_; |
| 27 | + }; |
| 28 | + sox_effect_t* operator->() noexcept { |
| 29 | + return se_; |
| 30 | + } |
| 31 | + |
| 32 | + private: |
| 33 | + sox_effect_t* se_; |
| 34 | +}; |
| 35 | + |
| 36 | +/// helper classes for passing the location of input tensor and output buffer |
| 37 | +/// |
| 38 | +/// drain/flow callback functions require plaing C style function signature and |
| 39 | +/// the way to pass extra data is to attach data to sox_fffect_t::priv pointer. |
| 40 | +/// The following structs will be assigned to sox_fffect_t::priv pointer which |
| 41 | +/// gives sox_effect_t an access to input Tensor and output buffer object. |
| 42 | +struct TensorInputPriv { |
| 43 | + size_t index; |
| 44 | + TensorSignal* signal; |
| 45 | +}; |
| 46 | +struct TensorOutputPriv { |
| 47 | + std::vector<sox_sample_t>* buffer; |
| 48 | +}; |
| 49 | + |
| 50 | +/// Callback function to feed Tensor data to SoxEffectChain. |
| 51 | +int tensor_input_drain(sox_effect_t* effp, sox_sample_t* obuf, size_t* osamp) { |
| 52 | + // Retrieve the input Tensor and current index |
| 53 | + auto priv = static_cast<TensorInputPriv*>(effp->priv); |
| 54 | + auto index = priv->index; |
| 55 | + auto signal = priv->signal; |
| 56 | + auto tensor = signal->getTensor(); |
| 57 | + auto num_channels = effp->out_signal.channels; |
| 58 | + |
| 59 | + // Adjust the number of samples to read |
| 60 | + const size_t num_samples = tensor.numel(); |
| 61 | + if (index + *osamp > num_samples) { |
| 62 | + *osamp = num_samples - index; |
| 63 | + } |
| 64 | + // Ensure that it's a multiple of the number of channels |
| 65 | + *osamp -= *osamp % num_channels; |
| 66 | + |
| 67 | + // Slice the input Tensor and unnormalize the values |
| 68 | + const auto tensor_ = [&]() { |
| 69 | + auto i_frame = index / num_channels; |
| 70 | + auto num_frames = *osamp / num_channels; |
| 71 | + auto t = (signal->getChannelsFirst()) |
| 72 | + ? tensor.index({Slice(), Slice(i_frame, i_frame + num_frames)}).t() |
| 73 | + : tensor.index({Slice(i_frame, i_frame + num_frames), Slice()}); |
| 74 | + return unnormalize_wav(t.reshape({-1})).contiguous(); |
| 75 | + }(); |
| 76 | + priv->index += *osamp; |
| 77 | + |
| 78 | + // Write data to SoxEffectsChain buffer. |
| 79 | + auto ptr = tensor_.data_ptr<int32_t>(); |
| 80 | + std::copy(ptr, ptr + *osamp, obuf); |
| 81 | + |
| 82 | + return (priv->index == num_samples) ? SOX_EOF : SOX_SUCCESS; |
| 83 | +} |
| 84 | + |
| 85 | +/// Callback function to fetch data from SoxEffectChain. |
| 86 | +int tensor_output_flow( |
| 87 | + sox_effect_t* effp LSX_UNUSED, |
| 88 | + sox_sample_t const* ibuf, |
| 89 | + sox_sample_t* obuf LSX_UNUSED, |
| 90 | + size_t* isamp, |
| 91 | + size_t* osamp) { |
| 92 | + *osamp = 0; |
| 93 | + // Get output buffer |
| 94 | + auto out_buffer = static_cast<TensorOutputPriv*>(effp->priv)->buffer; |
| 95 | + // Append at the end |
| 96 | + out_buffer->insert(out_buffer->end(), ibuf, ibuf + *isamp); |
| 97 | + return SOX_SUCCESS; |
| 98 | +} |
| 99 | + |
| 100 | +sox_effect_handler_t* get_tensor_input_handler() { |
| 101 | + static sox_effect_handler_t handler{/*name=*/"input_tensor", |
| 102 | + /*usage=*/NULL, |
| 103 | + /*flags=*/SOX_EFF_MCHAN, |
| 104 | + /*getopts=*/NULL, |
| 105 | + /*start=*/NULL, |
| 106 | + /*flow=*/NULL, |
| 107 | + /*drain=*/tensor_input_drain, |
| 108 | + /*stop=*/NULL, |
| 109 | + /*kill=*/NULL, |
| 110 | + /*priv_size=*/sizeof(TensorInputPriv)}; |
| 111 | + return &handler; |
| 112 | +} |
| 113 | + |
| 114 | +sox_effect_handler_t* get_tensor_output_handler() { |
| 115 | + static sox_effect_handler_t handler{/*name=*/"output_tensor", |
| 116 | + /*usage=*/NULL, |
| 117 | + /*flags=*/SOX_EFF_MCHAN, |
| 118 | + /*getopts=*/NULL, |
| 119 | + /*start=*/NULL, |
| 120 | + /*flow=*/tensor_output_flow, |
| 121 | + /*drain=*/NULL, |
| 122 | + /*stop=*/NULL, |
| 123 | + /*kill=*/NULL, |
| 124 | + /*priv_size=*/sizeof(TensorOutputPriv)}; |
| 125 | + return &handler; |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace |
| 129 | + |
| 130 | +SoxEffectsChain::SoxEffectsChain( |
| 131 | + sox_encodinginfo_t input_encoding, |
| 132 | + sox_encodinginfo_t output_encoding) |
| 133 | + : in_enc_(input_encoding), |
| 134 | + out_enc_(output_encoding), |
| 135 | + in_sig_(), |
| 136 | + interm_sig_(), |
| 137 | + sec_(sox_create_effects_chain(&in_enc_, &out_enc_)) { |
| 138 | + if (!sec_) { |
| 139 | + throw std::runtime_error("Failed to create effect chain."); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +SoxEffectsChain::~SoxEffectsChain() { |
| 144 | + if (sec_ != nullptr) { |
| 145 | + sox_delete_effects_chain(sec_); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +void SoxEffectsChain::run() { |
| 150 | + sox_flow_effects(sec_, NULL, NULL); |
| 151 | +} |
| 152 | + |
| 153 | +void SoxEffectsChain::addInputTensor(TensorSignal* signal) { |
| 154 | + in_sig_ = get_signalinfo(signal, "wav"); |
| 155 | + interm_sig_ = in_sig_; |
| 156 | + SoxEffect e(sox_create_effect(get_tensor_input_handler())); |
| 157 | + auto priv = static_cast<TensorInputPriv*>(e->priv); |
| 158 | + priv->signal = signal; |
| 159 | + priv->index = 0; |
| 160 | + if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) { |
| 161 | + throw std::runtime_error("Failed to add effect: input_tensor"); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void SoxEffectsChain::addOutputBuffer( |
| 166 | + std::vector<sox_sample_t>* output_buffer) { |
| 167 | + SoxEffect e(sox_create_effect(get_tensor_output_handler())); |
| 168 | + static_cast<TensorOutputPriv*>(e->priv)->buffer = output_buffer; |
| 169 | + if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) { |
| 170 | + throw std::runtime_error("Failed to add effect: output_tensor"); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +void SoxEffectsChain::addInputFile(sox_format_t* sf) { |
| 175 | + in_sig_ = sf->signal; |
| 176 | + interm_sig_ = in_sig_; |
| 177 | + SoxEffect e(sox_create_effect(sox_find_effect("input"))); |
| 178 | + char* opts[] = {(char*)sf}; |
| 179 | + sox_effect_options(e, 1, opts); |
| 180 | + if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) { |
| 181 | + std::ostringstream stream; |
| 182 | + stream << "Failed to add effect: input " << sf->filename; |
| 183 | + throw std::runtime_error(stream.str()); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +void SoxEffectsChain::addEffect(const std::vector<std::string> effect) { |
| 188 | + const auto num_args = effect.size(); |
| 189 | + if (num_args == 0) { |
| 190 | + throw std::runtime_error("Invalid argument: empty effect."); |
| 191 | + } |
| 192 | + const auto name = effect[0]; |
| 193 | + if (UNSUPPORTED_EFFECTS.find(name) != UNSUPPORTED_EFFECTS.end()) { |
| 194 | + std::ostringstream stream; |
| 195 | + stream << "Unsupported effect: " << name; |
| 196 | + throw std::runtime_error(stream.str()); |
| 197 | + } |
| 198 | + |
| 199 | + SoxEffect e(sox_create_effect(sox_find_effect(name.c_str()))); |
| 200 | + const auto num_options = num_args - 1; |
| 201 | + if (num_options == 0) { |
| 202 | + sox_effect_options(e, 0, nullptr); |
| 203 | + } else { |
| 204 | + std::vector<char*> opts; |
| 205 | + for (size_t i = 1; i < num_args; ++i) { |
| 206 | + opts.push_back((char*)effect[i].c_str()); |
| 207 | + } |
| 208 | + if (sox_effect_options(e, num_options, opts.data()) != SOX_SUCCESS) { |
| 209 | + std::ostringstream stream; |
| 210 | + stream << "Invalid effect option:"; |
| 211 | + for (const auto& v : effect) { |
| 212 | + stream << " " << v; |
| 213 | + } |
| 214 | + throw std::runtime_error(stream.str()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if (sox_add_effect(sec_, e, &interm_sig_, &in_sig_) != SOX_SUCCESS) { |
| 219 | + std::ostringstream stream; |
| 220 | + stream << "Failed to add effect: \"" << name; |
| 221 | + for (size_t i = 1; i < num_args; ++i) { |
| 222 | + stream << " " << effect[i]; |
| 223 | + } |
| 224 | + stream << "\""; |
| 225 | + throw std::runtime_error(stream.str()); |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +int64_t SoxEffectsChain::getOutputNumChannels() { |
| 230 | + return interm_sig_.channels; |
| 231 | +} |
| 232 | + |
| 233 | +int64_t SoxEffectsChain::getOutputSampleRate() { |
| 234 | + return interm_sig_.rate; |
| 235 | +} |
| 236 | + |
| 237 | +} // namespace sox_effects_chain |
| 238 | +} // namespace torchaudio |
0 commit comments