Stream Audio¶
Neon +2.8.31 +1.7
Using the receive_audio_frame method, you can receive audio frames, which you can use it to listen live, record, or perform real-time analysis like speech-to-text or sound analysis.
The data returned is an instance of AudioFrame.
AudioFrame(
av_frame=<av.AudioFrame pts=None, 1024 samples at 8000Hz, mono, fltp at 0x1189e0ac0>,
timestamp_unix_seconds=1758211800.8221593,
resampler=<av.audio.resampler.AudioResampler object at 0x1189e04c0>
)
AudioFrame
AudioFrame
¶
Bases: NamedTuple
An audio frame with timestamp information.
This class represents an audio frame from the audio stream with associated timestamp information. The Class inherits AudioFrame from py.av library.
Note
Audio in Neon is streamed as fltp mono 8K, this class takes the decoded packets as av.AudioFrames.
Methods:
-
to_ndarray–Convert the audio frame to a NumPy array.
-
to_resampled_ndarray–Convert the audio frame to a resampled s16 NumPy array
Attributes:
-
av_frame(AudioFrame) –The audio frame.
-
datetime(datetime) –Get timestamp as a datetime object.
-
resampler(AudioResampler) –A reference to a shared AudioResampler instance.
-
timestamp_unix_ns(int) –Get timestamp in nanoseconds since Unix epoch.
-
timestamp_unix_seconds(float) –Timestamp in seconds since Unix epoch.
resampler
instance-attribute
¶
resampler: AudioResampler
A reference to a shared AudioResampler instance.
timestamp_unix_seconds
instance-attribute
¶
timestamp_unix_seconds: float
Timestamp in seconds since Unix epoch.
to_ndarray
¶
Convert the audio frame to a NumPy array.
Source code in src/pupil_labs/realtime_api/streaming/audio.py
58 59 60 | |
to_resampled_ndarray
¶
Convert the audio frame to a resampled s16 NumPy array
Source code in src/pupil_labs/realtime_api/streaming/audio.py
62 63 64 65 | |
By default, audio is streamed in mono using the AAC codec. The stream is downsampled from the original 48 kHz source to a sampling rate of 8 kHz to save bandwidth, and uses a 32-bit floating-point planar (fltp) format.
The audio stream does not have it's own RTSP stream but is multiplexed with video, so we create a virtual sensor component using the Scene Camera stream.
Working with Audio Data¶
You can easily receive audio frames and convert them to NumPy arrays using the to_ndarray method and feed these to any audio library of your choice like librosa for analysis.
Check the whole example code here
| stream_audio.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Playing Audio¶
Audio Playback in realtime can be tricky, here we use SoundDevice. This library digest NumPy arrays and allows to play them back quickly. The only caveat is that it does not accept 32 bit planar audio format, thus, we have to resample it.
For commodity, we included a PyAv AudioResampler object to the AudioFrame class, it lazy loads, and calling to_resampled_ndarray will convert convert the av.AudioFrame to a NumPy array in signed 16-bit integer format, which is supported by SoundDevice for playback.
To simplify development and ensure audio does not block your application, the examples include an AudioPlayer class. It handles audio buffering and playback in a background thread, using a circular buffer to guarantee smooth playback without glitches or silence.
AudioPlayer
AudioPlayer
¶
Bases: Thread
A threaded, low-latency audio player using a shared RingBuffer.
Methods:
-
add_data–Directly write data to the shared RingBuffer.
-
close–Signal the thread to stop and clean up resources.
-
get_buffer_size–Get the current number of samples in the buffer for debugging.
-
run–Run the main entrypoint for the thread.
Source code in examples/audio_player.py
120 121 122 123 124 125 126 127 128 129 130 131 132 | |
add_data
¶
add_data(data: NDArray[int16]) -> None
Directly write data to the shared RingBuffer.
Source code in examples/audio_player.py
161 162 163 | |
close
¶
close() -> None
Signal the thread to stop and clean up resources.
Source code in examples/audio_player.py
169 170 171 172 173 174 | |
get_buffer_size
¶
get_buffer_size() -> int
Get the current number of samples in the buffer for debugging.
Source code in examples/audio_player.py
165 166 167 | |
run
¶
run() -> None
Run the main entrypoint for the thread.
Source code in examples/audio_player.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
Check the whole example code here
| stream_audio_and_play.py | |
|---|---|
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 | |
Playing Video and Audio¶
Here you can find an example that shows how to play both video with gaze overlay and audio using OpenCV and SoundDevice.
Check the whole example code here
| stream_video_gaze_and_audio.py | |
|---|---|
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
Bonus
On the Async API examples you can also find how to use the audio and plot the spectrum using librosa library.