Class: TorchCodec::Encoders::AudioEncoder
- Inherits:
-
Object
- Object
- TorchCodec::Encoders::AudioEncoder
- Defined in:
- lib/torchcodec/encoders/audio_encoder.rb
Instance Method Summary collapse
-
#initialize(samples, sample_rate:) ⇒ AudioEncoder
constructor
A new instance of AudioEncoder.
- #to_file(dest, bit_rate: nil, num_channels: nil, sample_rate: nil) ⇒ Object
- #to_tensor(format, bit_rate: nil, num_channels: nil, sample_rate: nil) ⇒ Object
Constructor Details
#initialize(samples, sample_rate:) ⇒ AudioEncoder
Returns a new instance of AudioEncoder.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/torchcodec/encoders/audio_encoder.rb', line 4 def initialize(samples, sample_rate:) # Some of these checks are also done in C++: it's OK, they're cheap, and # doing them here allows to surface them when the AudioEncoder is # instantiated, rather than later when the encoding methods are called. if !samples.is_a?(Torch::Tensor) raise ArgumentError, "Expected samples to be a Tensor, got #{samples.class.name}." end if samples.ndim == 1 # make it 2D and assume 1 channel samples = Torch.unsqueeze(samples, 0) end if samples.ndim != 2 raise ArgumentError, "Expected 1D or 2D samples, got #{samples.shape}." end if samples.dtype != Torch.float32 raise ArgumentError, "Expected float32 samples, got #{samples.dtype}." end if sample_rate <= 0 raise ArgumentError, "#{sample_rate} must be > 0." end @samples = samples @sample_rate = sample_rate end |
Instance Method Details
#to_file(dest, bit_rate: nil, num_channels: nil, sample_rate: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/torchcodec/encoders/audio_encoder.rb', line 29 def to_file( dest, bit_rate: nil, num_channels: nil, sample_rate: nil ) Core.encode_audio_to_file( @samples, @sample_rate, dest.to_s, bit_rate, num_channels, sample_rate ) end |
#to_tensor(format, bit_rate: nil, num_channels: nil, sample_rate: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/torchcodec/encoders/audio_encoder.rb', line 45 def to_tensor( format, bit_rate: nil, num_channels: nil, sample_rate: nil ) Core.encode_audio_to_tensor( @samples, @sample_rate, format, bit_rate, num_channels, sample_rate ) end |