68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
# audio_test.py
|
|||
|
|
import soundfile as sf
|
||
|
|
import numpy as np
|
||
|
|
import jack
|
||
|
|
import sys
|
||
|
|
|
||
|
|
def test_audio(filepath):
|
||
|
|
client = jack.Client("AudioTest")
|
||
|
|
sr = client.samplerate
|
||
|
|
|
||
|
|
out_L = client.outports.register("test_L")
|
||
|
|
out_R = client.outports.register("test_R")
|
||
|
|
|
||
|
|
# Load file
|
||
|
|
try:
|
||
|
|
data, file_sr = sf.read(filepath, always_2d=True)
|
||
|
|
data = data.T.astype(np.float32)
|
||
|
|
print(f"✓ Loaded: {data.shape} at {file_sr}Hz")
|
||
|
|
if file_sr != sr:
|
||
|
|
print(f"⚠ Sample rate mismatch: file={file_sr} JACK={sr}")
|
||
|
|
print(f" Install librosa for auto-resampling")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ Load failed: {e}")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
position = [0]
|
||
|
|
playing = [True]
|
||
|
|
|
||
|
|
def callback(frames):
|
||
|
|
out_L.get_array().fill(0.0)
|
||
|
|
out_R.get_array().fill(0.0)
|
||
|
|
|
||
|
|
if playing[0]:
|
||
|
|
pos = position[0]
|
||
|
|
end = min(pos + frames, data.shape[1])
|
||
|
|
chunk = data[:, pos:end]
|
||
|
|
n = chunk.shape[1]
|
||
|
|
|
||
|
|
out_L.get_array()[:n] = chunk[0]
|
||
|
|
out_R.get_array()[:n] = chunk[1] if data.shape[0] > 1 else chunk[0]
|
||
|
|
|
||
|
|
position[0] += n
|
||
|
|
if position[0] >= data.shape[1]:
|
||
|
|
playing[0] = False
|
||
|
|
print("✓ Playback complete")
|
||
|
|
|
||
|
|
client.set_process_callback(callback)
|
||
|
|
client.activate()
|
||
|
|
|
||
|
|
print(f"Playing {filepath}")
|
||
|
|
print("Wire 'AudioTest:test_L/R' in qpwgraph to hear audio")
|
||
|
|
print("Press Ctrl+C to stop")
|
||
|
|
|
||
|
|
import time
|
||
|
|
try:
|
||
|
|
while playing[0]:
|
||
|
|
time.sleep(0.1)
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
pass
|
||
|
|
|
||
|
|
client.deactivate()
|
||
|
|
client.close()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
if len(sys.argv) < 2:
|
||
|
|
print("Usage: python audio_test.py /path/to/file.flac")
|
||
|
|
sys.exit(1)
|
||
|
|
test_audio(sys.argv[1])
|