-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial_audio.py
More file actions
70 lines (54 loc) · 2.15 KB
/
spatial_audio.py
File metadata and controls
70 lines (54 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
"""
Spatial Audio Example
This example demonstrates the use of the PannerNode to create a 3D audio experience.
It creates a sound source that moves in a circle around the listener.
"""
import time
import math
import numpy as np
from labsound import AudioContext
# Create an audio context
with AudioContext() as context:
# Create an oscillator as our sound source
oscillator = context.create_oscillator()
oscillator.frequency.value = 440 # A4 note
oscillator.type = "sine"
# Create a panner node for 3D positioning
panner = context.create_panner()
# Set panner properties
panner.panning_model = "HRTF" # Head-related transfer function for realistic 3D audio
panner.distance_model = "inverse"
panner.ref_distance = 1
panner.max_distance = 10000
panner.rolloff_factor = 1
# Connect the oscillator to the panner, and the panner to the destination
context.connect(oscillator, panner)
context.connect(panner, context.destination)
# Start the oscillator
oscillator.start()
print("Moving sound source in a circle around the listener...")
print("Press Ctrl+C to stop")
try:
# Move the sound source in a circle around the listener
radius = 5 # 5 units away from the listener
for i in range(1000): # Run for a while
# Calculate position on the circle
angle = (i * 0.05) % (2 * math.pi)
x = radius * math.sin(angle)
z = radius * math.cos(angle)
y = 0 # Keep at the same height as the listener
# Update the panner position
panner.set_position(x, y, z)
# Print the current position (only occasionally to avoid console spam)
if i % 10 == 0:
print(f"Sound source position: ({x:.2f}, {y:.2f}, {z:.2f})")
# Wait a bit before the next update
time.sleep(0.05)
except KeyboardInterrupt:
print("\nStopping...")
finally:
# Stop the oscillator
oscillator.stop()
# Wait a moment for the sound to finish
time.sleep(0.5)
print("Done!")