Spaces:
Sleeping
Sleeping
Download tests/test_stream.py from JonCard/infiniteradio: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/JonCard/infiniteradio/resolve/main/tests/test_stream.py
- Command line
-
hf download hf://spaces/JonCard/infiniteradio/tests/test_stream.py
-
curl -L -o test_stream.py https://huggingface.co/spaces/JonCard/infiniteradio/resolve/main/tests/test_stream.py
1.15 kB
| MP3_SAMPLE_RATES = {0b00: 44100, 0b01: 48000, 0b10: 32000} | |
| def mp3_frame_headers(data: bytes): | |
| """Yields (bitrate_index, sample_rate, channel_mode) for MPEG-1 Layer III | |
| frame headers found by syncword.""" | |
| for i in range(len(data) - 3): | |
| if data[i] == 0xFF and data[i + 1] & 0xFE == 0xFA: | |
| bitrate_index = data[i + 2] >> 4 | |
| rate = MP3_SAMPLE_RATES.get((data[i + 2] >> 2) & 0b11) | |
| if 0 < bitrate_index < 15 and rate: | |
| yield bitrate_index, rate, data[i + 3] >> 6 | |
| def test_stream_is_48khz_stereo_mp3(client): | |
| with client.stream("GET", "/stream") as response: | |
| assert response.status_code == 200 | |
| assert response.headers["content-type"] == "audio/mpeg" | |
| data = b"" | |
| for piece in response.iter_bytes(): | |
| data += piece | |
| if len(data) > 16_000: | |
| break | |
| headers = list(mp3_frame_headers(data)) | |
| assert len(headers) >= 10 | |
| # Most syncword matches are real frames: 128 kbps, 48 kHz, not mono (mode 3). | |
| real = [h for h in headers if h[:2] == (9, 48000) and h[2] != 3] | |
| assert len(real) >= 0.9 * len(headers) | |