Module: TorchAudio
- Defined in:
- lib/torchaudio.rb,
lib/torchaudio/version.rb,
lib/torchaudio/functional.rb,
lib/torchaudio/datasets/utils.rb,
lib/torchaudio/datasets/yesno.rb,
lib/torchaudio/transforms/vol.rb,
lib/torchaudio/transforms/fade.rb,
lib/torchaudio/transforms/mfcc.rb,
lib/torchaudio/transforms/mel_scale.rb,
lib/torchaudio/transforms/spectrogram.rb,
lib/torchaudio/transforms/compute_deltas.rb,
lib/torchaudio/transforms/amplitude_to_db.rb,
lib/torchaudio/transforms/mel_spectrogram.rb,
lib/torchaudio/transforms/mu_law_decoding.rb,
lib/torchaudio/transforms/mu_law_encoding.rb
Defined Under Namespace
Modules: Datasets, Functional, Transforms Classes: Error
Constant Summary collapse
- VERSION =
"0.6.0"- F =
Functional
Class Method Summary collapse
- .load(uri, frame_offset: 0, num_frames: -1,, channels_first: true) ⇒ Object
- .load_wav(path, channels_first: true) ⇒ Object
- .save(uri, src, sample_rate, channels_first: true, compression: nil) ⇒ Object
Class Method Details
.load(uri, frame_offset: 0, num_frames: -1,, channels_first: true) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/torchaudio.rb', line 30 def load( uri, frame_offset: 0, num_frames: -1, channels_first: true ) begin require "torchcodec" rescue LoadError raise LoadError, "TorchCodec is required for load. Please install torchcodec to use this function." end begin decoder = TorchCodec::Decoders::AudioDecoder.new(uri) rescue => e raise RuntimeError, "Failed to create AudioDecoder for #{uri}: #{e}" end # Get sample rate from metadata sample_rate = decoder.[:sample_rate] if sample_rate.nil? raise RuntimeError, "Unable to determine sample rate from audio metadata" end # Decode the entire file first, then subsample manually # This is the simplest approach since torchcodec uses time-based indexing begin audio_samples = decoder.get_all_samples rescue => e raise RuntimeError, "Failed to decode audio samples: #{e}" end data = audio_samples[:data] # Apply frame_offset and num_frames (which are actually sample offsets) if frame_offset > 0 if frame_offset >= data.shape[1] # Return empty tensor if offset is beyond available data empty_shape = channels_first ? [data.shape[0], 0] : [0, data.shape[0]] return [Torch.zeros(empty_shape, dtype: Torch.float32), sample_rate] end data = data[0.., frame_offset..] end if num_frames == 0 # Return empty tensor if num_frames is 0 empty_shape = channels_first ? [data.shape[0], 0] : [0, data.shape[0]] return [Torch.zeros(empty_shape, dtype: Torch.float32), sample_rate] elsif num_frames > 0 data = data[0.., 0...num_frames] end # TorchCodec returns data in [channel, time] format by default # Handle channels_first parameter if !channels_first data = data.transpose(0, 1) # [channel, time] -> [time, channel] end [data, sample_rate] end |
.load_wav(path, channels_first: true) ⇒ Object
91 92 93 |
# File 'lib/torchaudio.rb', line 91 def load_wav(path, channels_first: true) load(path, channels_first: channels_first) end |
.save(uri, src, sample_rate, channels_first: true, compression: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/torchaudio.rb', line 95 def save( uri, src, sample_rate, channels_first: true, compression: nil ) begin require "torchcodec" rescue LoadError raise LoadError, "TorchCodec is required for save. Please install torchcodec to use this function." end # Input validation if !src.is_a?(Torch::Tensor) raise ArgumentError, "Expected src to be a torch.Tensor, got #{src.class.name}" end if src.dtype != Torch.float32 src = src.float end if sample_rate <= 0 raise ArgumentError, "sample_rate must be positive, got #{sample_rate}" end # Handle tensor shape and channels_first if src.ndim == 1 # Convert to 2D: [1, time] for channels_first: true if channels_first data = src.unsqueeze(0) # [1, time] else # For channels_first: false, input is [time] -> reshape to [time, 1] -> transpose to [1, time] data = src.unsqueeze(1).transpose(0, 1) # [time, 1] -> [1, time] end elsif src.ndim == 2 if channels_first data = src # Already [channel, time] else data = src.transpose(0, 1) # [time, channel] -> [channel, time] end else raise ArgumentError, "Expected 1D or 2D tensor, got #{src.ndim}D tensor" end # Create AudioEncoder begin encoder = TorchCodec::Encoders::AudioEncoder.new(data, sample_rate: sample_rate) rescue => e raise RuntimeError, "Failed to create AudioEncoder: #{e}" end # Determine bit_rate from compression parameter bit_rate = nil if !compression.nil? if compression.is_a?(Integer) || compression.is_a?(Float) bit_rate = compression.to_i else warn "Unsupported compression type #{compression.class.name}." end end # Save to file begin encoder.to_file(uri, bit_rate: bit_rate) rescue => e raise RuntimeError, "Failed to save audio to #{uri}: #{e}" end end |