Skip to content

Instantly share code, notes, and snippets.

@elibroftw
Last active December 20, 2022 21:37
Show Gist options
  • Save elibroftw/0fc6ed102dbe3f99863829a7e989dcc2 to your computer and use it in GitHub Desktop.
Save elibroftw/0fc6ed102dbe3f99863829a7e989dcc2 to your computer and use it in GitHub Desktop.
Generate sound waves for any audio file
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