Skip to content

Instantly share code, notes, and snippets.

@Staars
Last active February 22, 2024 13:21
Show Gist options
  • Save Staars/aa45e08b573c2f363afb0d57012e8b99 to your computer and use it in GitHub Desktop.
Save Staars/aa45e08b573c2f363afb0d57012e8b99 to your computer and use it in GitHub Desktop.
pyogg opus test
# Convert raw data from Alexa remote control - Bluetooth
import wave
from pyogg import OpusDecoder
if __name__ == "__main__":
# Setup decoding
# ==============
# Create an Opus decoder
opus_decoder = OpusDecoder()
channels = 1
opus_decoder.set_channels(channels)
samples_per_second = 16000
opus_decoder.set_sampling_frequency(samples_per_second)
# Open an output wav for the decoded PCM
output_filename = "output.wav"
wave_write = wave.open(output_filename, "wb")
print("Writing wav into file '{:s}'".format(output_filename))
# Save the wav's specification
wave_write.setnchannels(channels)
wave_write.setframerate(samples_per_second)
bytes_per_sample = 2
wave_write.setsampwidth(bytes_per_sample)
# Execute decode
# =====================
# Loop through the raw file's opus data and decode it, the save it as wav file
packet_pos = 0
opus_file = open("audio.raw","rb")
opus_buffer = bytearray(opus_file.read())
opus_file.close()
while packet_pos < len(opus_buffer):
# decode this in 80-bytes-steps
# using OpusDecoder.
decoded_pcm = opus_decoder.decode(opus_buffer[packet_pos:packet_pos+80])
print("frame done:", packet_pos/80)
packet_pos += 80
# Save the decoded PCM as a new wav filepython3
wave_write.writeframes(decoded_pcm)
wave_write.close()
print("Finished.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment