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 play them 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, the audio signal 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 in this client, 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, on the examples we propose SoundDevice. This library digest NumPy arrays and allows to play them back quickly, with the only caveat 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. We also 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, you can find it in the audio_player.py file.
You can find a simple example below that streams audio and plays it back using the AudioPlayer class.
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 src/pupil_labs/realtime_api/audio_player.py
112 113 114 115 116 117 118 119 120 121 122 123 124 | |
add_data
¶
add_data(data: NDArray[int16]) -> None
Directly write data to the shared RingBuffer.
Source code in src/pupil_labs/realtime_api/audio_player.py
155 156 157 | |
close
¶
close() -> None
Signal the thread to stop and clean up resources.
Source code in src/pupil_labs/realtime_api/audio_player.py
163 164 165 166 167 168 | |
get_buffer_size
¶
get_buffer_size() -> int
Get the current number of samples in the buffer for debugging.
Source code in src/pupil_labs/realtime_api/audio_player.py
159 160 161 | |
run
¶
run() -> None
Run the main entrypoint for the thread.
Source code in src/pupil_labs/realtime_api/audio_player.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
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 | |
Note
Now, you can also use a different audio library like PyAudio or pygame to play back the audio data, but you might need to install portaudio, and the latter is more suited for game development.
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 the AudioPlayer class.
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 | |
Bonus
On the Async API examples you can also find how to use the audio and plot the spectrum using librosa library.