Created
March 26, 2025 18:58
-
-
Save hamees-sayed/bf35a15c24c412c9cafe3e4dbc628dcb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import soundfile as sf | |
import matplotlib.pyplot as plt | |
def encode_mu_law(audio_data, mu=255): | |
max_val = np.max(np.abs(audio_data)) | |
normalized_audio = audio_data / max_val | |
encoded = np.sign(normalized_audio) * np.log(1 + mu * np.abs(normalized_audio)) / np.log(1 + mu) | |
encoded_int16 = (encoded * 32767).astype(np.int16) | |
print("Encoded stats: min={}, max={}, mean={}".format( | |
np.min(encoded_int16), np.max(encoded_int16), np.mean(encoded_int16))) | |
return encoded_int16, max_val | |
def decode_mu_law(encoded_audio, max_val, mu=255): | |
normalized_encoded = encoded_audio.astype(np.float32) / 32767.0 | |
decoded = np.sign(normalized_encoded) * (1/mu) * ((1 + mu)**np.abs(normalized_encoded) - 1) | |
decoded_audio = decoded * max_val | |
return decoded_audio | |
def process_wav_file(input_path, output_encoded_path, output_decoded_path): | |
audio_data, sample_rate = sf.read(input_path) | |
print("Input audio stats: min={}, max={}, mean={}".format( | |
np.min(audio_data), np.max(audio_data), np.mean(audio_data))) | |
mu_law_encoded, max_val = encode_mu_law(audio_data) | |
print(type(mu_law_encoded)) | |
sf.write(output_encoded_path, mu_law_encoded, sample_rate, subtype='PCM_16') | |
np.save(output_encoded_path + '.maxval', max_val) | |
decoded_audio = decode_mu_law(mu_law_encoded, max_val) | |
sf.write(output_decoded_path, decoded_audio, sample_rate) | |
print("Decoded audio stats: min={}, max={}, mean={}".format( | |
np.min(decoded_audio), np.max(decoded_audio), np.mean(decoded_audio))) | |
plt.figure(figsize=(12, 6)) | |
plt.subplot(2, 1, 1) | |
plt.plot(audio_data) | |
plt.title("Original Audio") | |
plt.subplot(2, 1, 2) | |
plt.plot(decoded_audio) | |
plt.title("Decoded Audio (After μ-law round-trip)") | |
plt.tight_layout() | |
plt.show() | |
process_wav_file("input.wav", "output_mu_encoded.wav", "output_decoded.wav") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment