effects_chain.h 1.75 KB
Newer Older
moto's avatar
moto committed
1
2
3
#ifndef TORCHAUDIO_SOX_EFFECTS_CHAIN_H
#define TORCHAUDIO_SOX_EFFECTS_CHAIN_H

4
#include <libtorchaudio/sox/utils.h>
moto's avatar
moto committed
5
6
#include <sox.h>

Moto Hira's avatar
Moto Hira committed
7
namespace torchaudio::sox {
moto's avatar
moto committed
8

9
10
11
12
13
14
// Helper struct to safely close sox_effect_t* pointer returned by
// sox_create_effect

struct SoxEffect {
  explicit SoxEffect(sox_effect_t* se) noexcept;
  SoxEffect(const SoxEffect& other) = delete;
moto-meta's avatar
moto-meta committed
15
  SoxEffect(SoxEffect&& other) = delete;
hwangjeff's avatar
hwangjeff committed
16
17
  auto operator=(const SoxEffect& other) -> SoxEffect& = delete;
  auto operator=(SoxEffect&& other) -> SoxEffect& = delete;
18
19
  ~SoxEffect();
  operator sox_effect_t*() const;
hwangjeff's avatar
hwangjeff committed
20
  auto operator->() noexcept -> sox_effect_t*;
21
22
23
24
25

 private:
  sox_effect_t* se_;
};

moto's avatar
moto committed
26
27
28
29
// Helper struct to safely close sox_effects_chain_t with handy methods
class SoxEffectsChain {
  const sox_encodinginfo_t in_enc_;
  const sox_encodinginfo_t out_enc_;
30
31

 protected:
moto's avatar
moto committed
32
33
  sox_signalinfo_t in_sig_;
  sox_signalinfo_t interm_sig_;
34
  sox_signalinfo_t out_sig_;
moto's avatar
moto committed
35
36
37
38
39
40
41
  sox_effects_chain_t* sec_;

 public:
  explicit SoxEffectsChain(
      sox_encodinginfo_t input_encoding,
      sox_encodinginfo_t output_encoding);
  SoxEffectsChain(const SoxEffectsChain& other) = delete;
moto-meta's avatar
moto-meta committed
42
  SoxEffectsChain(SoxEffectsChain&& other) = delete;
moto's avatar
moto committed
43
44
45
46
  SoxEffectsChain& operator=(const SoxEffectsChain& other) = delete;
  SoxEffectsChain& operator=(SoxEffectsChain&& other) = delete;
  ~SoxEffectsChain();
  void run();
47
48
49
50
  void addInputTensor(
      torch::Tensor* waveform,
      int64_t sample_rate,
      bool channels_first);
moto's avatar
moto committed
51
52
  void addInputFile(sox_format_t* sf);
  void addOutputBuffer(std::vector<sox_sample_t>* output_buffer);
53
  void addOutputFile(sox_format_t* sf);
moto-meta's avatar
moto-meta committed
54
  void addEffect(const std::vector<std::string>& effect);
moto's avatar
moto committed
55
56
57
58
  int64_t getOutputNumChannels();
  int64_t getOutputSampleRate();
};

Moto Hira's avatar
Moto Hira committed
59
} // namespace torchaudio::sox
moto's avatar
moto committed
60
61

#endif