Guest User

Untitled

a guest
Feb 17th, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import soundfile as sf
  3. import json
  4. import subprocess
  5. import os
  6.  
  7. def mel_frequencies(n_mels=128, fmin=0.0, fmax=8000.0):
  8.     """Create a list of frequencies uniformly spaced on the Mel scale."""
  9.     fmin_mel = 2595.0 * np.log10(1.0 + fmin / 700.0)
  10.     fmax_mel = 2595.0 * np.log10(1.0 + fmax / 700.0)
  11.     mels = np.linspace(fmin_mel, fmax_mel, num=n_mels)
  12.     freqs = 700.0 * (10.0**(mels / 2595.0) - 1.0)
  13.     return freqs
  14.  
  15. def update_waybar():
  16.     # Update the Waybar module with the new data
  17.     subprocess.run(['pkill', '-SIGRTMIN+1', 'waybar'])
  18.  
  19. home_dir = os.path.expanduser('~')
  20. audio_file_path = os.path.join(home_dir, 'audio.wav')
  21.  
  22. data, samplerate = sf.read(audio_file_path)
  23. fft = np.fft.rfft(data, axis=0)
  24. fft_mean = np.mean(np.abs(fft), axis=1)
  25.  
  26. num_bins = 5
  27. mel_freqs = mel_frequencies(n_mels=num_bins+2, fmin=0, fmax=samplerate/2)  # Ensure fmax does not exceed Nyquist frequency
  28. bins = np.round((len(fft_mean) - 1) * mel_freqs / (samplerate/2)).astype(int)
  29. fft_mean_binned = np.add.reduceat(fft_mean, bins[:-1]) / np.diff(bins)
  30. fft_mean_binned = np.nan_to_num(fft_mean_binned)
  31.  
  32. # Normalize the binned data to the range 1-8 (inclusive)
  33. normalized_bins = np.interp(fft_mean_binned, (fft_mean_binned.min(), fft_mean_binned.max()), (1, 8))
  34.  
  35. # Convert the normalized data to block characters
  36. block_chars = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
  37. visualizer_string = ''.join(block_chars[int(round(bin_value))] for bin_value in normalized_bins)
  38.  
  39. # Convert the visualizer string to JSON
  40. json_data = json.dumps({"data": visualizer_string})
  41.  
  42. # Print the JSON data
  43. print(json_data)
  44.  
Add Comment
Please, Sign In to add comment