Views
No views yet
pip install onnxruntime numpy1import numpy as np
2import onnxruntime as ort
3
4# Load both models
5encoder = ort.InferenceSession('encoder.onnx')
6decoder = ort.InferenceSession('decoder.onnx')
7
8# Helper function for padding
9def pad_audio_to_frames(audio, samples_per_frame=1764):
10 """Pad audio to complete frames (required for encoder)."""
11 current_len = audio.shape[-1]
12 num_frames = (current_len + samples_per_frame - 1) // samples_per_frame
13 padded_len = num_frames * samples_per_frame
14 padding_needed = padded_len - current_len
15 if padding_needed > 0:
16 audio = np.pad(audio, ((0, 0), (0, padding_needed)), mode='constant')
17 return audio, current_len
18
19# Load your audio (must be 22050 Hz, mono)
20original_audio = np.random.randn(1, 88200).astype(np.float32) # 4 seconds example
21
22# 1. Pad audio to complete frames
23padded_audio, original_len = pad_audio_to_frames(original_audio)
24audio_len = np.array([padded_audio.shape[-1]], dtype=np.int64)
25
26# 2. Encode: Audio → Tokens
27tokens, tokens_len = encoder.run(None, {
28 'audio': padded_audio,
29 'audio_len': audio_len
30})
31
32# 3. Decode: Tokens → Audio
33reconstructed_audio, reconstructed_len = decoder.run(None, {
34 'tokens': tokens,
35 'tokens_len': tokens_len
36})
37
38# 4. Trim to original length
39reconstructed_audio = reconstructed_audio[:, :original_len]