Created
November 18, 2022 06:55
-
-
Save tzechienchu/1fafde22c75f54031f362913f10bcba8 to your computer and use it in GitHub Desktop.
Wave file to Bin for Embedded System
This file contains 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 wave | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import array | |
wav_obj = wave.open('spike11K.wav', 'rb') | |
sample_freq = wav_obj.getframerate() | |
print(sample_freq) | |
n_samples = wav_obj.getnframes() | |
print(n_samples) | |
t_audio = n_samples/sample_freq | |
print(t_audio) | |
n_channels = wav_obj.getnchannels() | |
print(n_channels) | |
signal_wave = wav_obj.readframes(n_samples) | |
signal_array = np.frombuffer(signal_wave, dtype=np.int16) | |
l_channel = signal_array[0::1] | |
times = np.linspace(0, n_samples/sample_freq, num=n_samples) | |
# plt.figure(figsize=(15, 5)) | |
# plt.plot(times, l_channel) | |
# plt.title('Left Channel') | |
# plt.ylabel('Signal Value') | |
# plt.xlabel('Time (s)') | |
# plt.xlim(0, t_audio) | |
# plt.show() | |
buf128K = array.array('H', (0 for i in range(64*1024))) #2Byte * 64K | |
print(l_channel[100:500]) | |
sub_a = l_channel[0:64*1024] | |
sub_b = l_channel[64*1024:64*1024+64*1024] | |
for idx, d in enumerate(sub_a): | |
buf128K[idx] = (d + 0x8000) >> 4 | |
print(buf128K[100:500]) | |
t_audio = (64*1024)/sample_freq | |
print(t_audio) | |
plt.figure(figsize=(15, 5)) | |
#plt.plot(times[0:64*1024], l_channel[0:64*1024]) | |
plt.plot(times[0:64*1024], buf128K) | |
plt.title('Left Channel') | |
plt.ylabel('Signal Value') | |
plt.xlabel('Time (s)') | |
plt.xlim(0, t_audio) | |
plt.show() | |
newFile = open("spike11KA.bin", "wb") | |
newFile.write(buf128K) | |
# plt.figure(figsize=(15, 5)) | |
# plt.specgram(l_channel, Fs=sample_freq, vmin=-20, vmax=50) | |
# plt.title('Left Channel') | |
# plt.ylabel('Frequency (Hz)') | |
# plt.xlabel('Time (s)') | |
# plt.xlim(0, t_audio) | |
# plt.colorbar() | |
# plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment