Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import soundfile as sf
- import json
- import subprocess
- import os
- def mel_frequencies(n_mels=128, fmin=0.0, fmax=8000.0):
- """Create a list of frequencies uniformly spaced on the Mel scale."""
- fmin_mel = 2595.0 * np.log10(1.0 + fmin / 700.0)
- fmax_mel = 2595.0 * np.log10(1.0 + fmax / 700.0)
- mels = np.linspace(fmin_mel, fmax_mel, num=n_mels)
- freqs = 700.0 * (10.0**(mels / 2595.0) - 1.0)
- return freqs
- def update_waybar():
- # Update the Waybar module with the new data
- subprocess.run(['pkill', '-SIGRTMIN+1', 'waybar'])
- home_dir = os.path.expanduser('~')
- audio_file_path = os.path.join(home_dir, 'audio.wav')
- data, samplerate = sf.read(audio_file_path)
- fft = np.fft.rfft(data, axis=0)
- fft_mean = np.mean(np.abs(fft), axis=1)
- num_bins = 5
- mel_freqs = mel_frequencies(n_mels=num_bins+2, fmin=0, fmax=samplerate/2) # Ensure fmax does not exceed Nyquist frequency
- bins = np.round((len(fft_mean) - 1) * mel_freqs / (samplerate/2)).astype(int)
- fft_mean_binned = np.add.reduceat(fft_mean, bins[:-1]) / np.diff(bins)
- fft_mean_binned = np.nan_to_num(fft_mean_binned)
- # Normalize the binned data to the range 1-8 (inclusive)
- normalized_bins = np.interp(fft_mean_binned, (fft_mean_binned.min(), fft_mean_binned.max()), (1, 8))
- # Convert the normalized data to block characters
- block_chars = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
- visualizer_string = ''.join(block_chars[int(round(bin_value))] for bin_value in normalized_bins)
- # Convert the visualizer string to JSON
- json_data = json.dumps({"data": visualizer_string})
- # Print the JSON data
- print(json_data)
Add Comment
Please, Sign In to add comment