Last active
December 20, 2022 21:37
-
-
Save elibroftw/0fc6ed102dbe3f99863829a7e989dcc2 to your computer and use it in GitHub Desktop.
Generate sound waves for any audio file
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 soundfile as sf | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import io | |
from PIL import Image | |
def get_audio_wave(file): | |
data, samplerate = sf.read(file) | |
n = len(data) | |
time_axis = np.linspace(0, n / samplerate, n, endpoint=False) | |
ch1, ch2 = data.transpose() | |
sound_axis = ch1 + ch2 | |
accent_color = '#00bfff' | |
bg = '#121212' | |
buf = io.BytesIO() | |
fig = plt.figure(figsize=(4.5 * 60, .75 * 60), dpi=5) | |
plt.plot(time_axis, sound_axis, color=accent_color) | |
plt.axis('off') | |
plt.margins(x=0) | |
fig.patch.set_facecolor(bg) | |
plt.savefig(buf, format='png', bbox_inches='tight', transparent=True) | |
im = Image.open(buf) | |
# im = im.resize((int(im.size[0] / 3), int(im.size[1] / 3))) | |
return im | |
# Usage | |
# test_file = 'C:/Users/maste/MEGA/Music/No Mana - Memories of Nothing.flac' | |
# get_audio_wave(test_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment