[fix] update for librosa 0.7.2

This commit is contained in:
alban 2020-09-28 14:40:39 +02:00
parent 678aae0c94
commit 7f42b10b95
3 changed files with 43 additions and 44 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.*.swp

View File

@ -11,10 +11,12 @@ from __future__ import print_function
import argparse
import json
import librosa
import math
import numpy
import os
import pyaudio
import redis
import statistics
import sys
import time
@ -25,9 +27,9 @@ def debug(*args, **kwargs):
# Define default variables.
_BAND_RANGE = 7
BAND_OCTAVES = 10 # 12 * 9 octaves
_BAND_TONES = BAND_OCTAVES * 12 # octaves * notes per octave
_CHANNELS = 1
_ENERGY_THRESHOLD = 0.1
_FRAMES_PER_BUFFER = 4410
_N_FFT = 4096
_RATE = 44100
@ -43,7 +45,6 @@ parser.add_argument('--device','-d', required=False, type=int, help='Which pyaud
parser.add_argument('--sampling-frequency','-s', required=False, default=0.1, type=float, help='Which frequency, in seconds. Default={}f '.format(_SAMPLING_FREQUENCY))
parser.add_argument('--channels','-c', required=False, default=_CHANNELS, type=int, help='How many channels. Default={} '.format(_CHANNELS))
parser.add_argument('--rate','-r', required=False, default=44100, type=int, help='Which rate. Default={} '.format(_RATE))
parser.add_argument('--energy-threshold','-e', required=False, default=0.4, type=float, help='Which energy triggers spectrum detection flag. Default={} '.format(_ENERGY_THRESHOLD))
# Redis Args
parser.add_argument("-i","--ip",help="IP address of the Redis server ",default="127.0.0.1",type=str)
parser.add_argument("-p","--port",help="Port of the Redis server ",default="6379",type=str)
@ -51,11 +52,15 @@ parser.add_argument("-p","--port",help="Port of the Redis server ",default="6379
parser.add_argument("-v","--verbose",action="store_true",help="Verbose")
args = parser.parse_args()
# global
bpm = 120.0
# Set real variables
BAND_RANGE = _BAND_RANGE
F_LO = librosa.note_to_hz('C0')
F_HI = librosa.note_to_hz('C10')
BAND_TONES = _BAND_TONES
CHANNELS = args.channels
DEVICE = args.device
ENERGY_THRESHOLD = args.energy_threshold
FRAMES_PER_BUFFER = int(args.rate * args.sampling_frequency )
LIST_DEVICES = args.list_devices
MODE = args.mode
@ -66,17 +71,11 @@ ip = args.ip
port = args.port
verbose = args.verbose
debug( "frames", FRAMES_PER_BUFFER)
if( MODE == "bpm" and RATE < 0.5 ):
debug( "You should use a --rate superior to 0.5 in BPM mode...")
if( MODE == "bpm" and SAMPLING_FREQUENCY < 0.5 ):
debug( "You should use a --sampling_frequency superior to 0.5 in BPM mode...")
# Define the frequency range of the log-spectrogram.
F_LO = librosa.note_to_hz('C2')
F_HI = librosa.note_to_hz('C9')
melFilter = librosa.filters.mel(RATE, N_FFT, BAND_RANGE, fmin=F_LO, fmax=F_HI)
melFilter = librosa.filters.mel(RATE, N_FFT, BAND_TONES, fmin=F_LO, fmax=F_HI)
r = redis.Redis(
host=ip,
@ -90,12 +89,12 @@ def list_devices():
p = pyaudio.PyAudio()
i = 0
n = p.get_device_count()
debug("\nFound {} devices\n".format(n))
debug (" {} {}".format('ID', 'Device name'))
print("\nFound {} devices\n".format(n))
print(" {} {}".format('ID', 'Device name'))
while i < n:
dev = p.get_device_info_by_index(i)
if dev['maxInputChannels'] > 0:
debug (" {} {}".format(i, dev['name']))
print(" {} {}".format(i, dev['name']))
i += 1
if( LIST_DEVICES ):
list_devices()
@ -103,8 +102,6 @@ if( LIST_DEVICES ):
p = pyaudio.PyAudio()
# global
bpm = 120.0
def m_bpm(audio_data):
"""
@ -140,9 +137,6 @@ def m_bpm(audio_data):
def m_spectrum(audio_data):
"""
This function saves fast analysis to redis
* spectrum
* RMS
* tuning
"""
# Compute real FFT.
@ -151,31 +145,34 @@ def m_spectrum(audio_data):
# Compute mel spectrum.
melspectrum = melFilter.dot(abs(fft))
# Get RMS
rms = librosa.feature.rmse( S=melspectrum, frame_length=FRAMES_PER_BUFFER )
# Initialize output characters to display.
bit_list = [0]*BAND_RANGE
count = 0
highest_index = -1
highest_value = 0
for i in range(BAND_RANGE):
val = melspectrum[i]
# If this is the highest tune, record it
if( val > highest_value ) :
highest_index = i
highest_value = val
spectrum_120 = [0]*BAND_TONES
spectrum_10 = [0]*BAND_OCTAVES
spectrum_oct = [[] for i in range(10)]
# If there is energy in this frequency, mark it
if val > ENERGY_THRESHOLD:
count += 1
bit_list[i] = val
# Assign values
for i in range(BAND_TONES):
val = round(melspectrum[i],2)
spectrum_120[i] = val
key = int(math.floor( i / 12 ))
spectrum_oct[key].append(val)
for i in range(BAND_OCTAVES):
spectrum_10[i] = round(sum( spectrum_oct[i] ) / len( spectrum_oct[i]),2)
# Get RMS
#rms = librosa.feature.rms( S=melspectrum )
rms = librosa.feature.rms( y=audio_data ).tolist()[0]
rms_avg = round(sum(rms) / len(rms),2)
# Save to redis
debug( 'rms:{} bit_list:{} highest_index:{}'.format(rms , bit_list, highest_index ))
r.set( 'rms', "{}".format(rms.tolist()) )
r.set( 'spectrum', json.dumps( bit_list ) )
r.set( 'tuning', highest_index )
#debug( 'spectrum_120:{} '.format(spectrum_120))
#debug( 'spectrum_10:{}'.format(spectrum_10))
#debug( 'rms:{}'.format(rms_avg))
r.set( 'spectrum_120', json.dumps( spectrum_120 ) )
r.set( 'spectrum_10', json.dumps( spectrum_10 ) )
r.set( 'rms', "{}".format(rms_avg) )
return True
@ -191,7 +188,7 @@ def callback(in_data, frame_count, time_info, status):
debug( "Unknown mode. Exiting")
os._exit(2)
end = time.time()
# debug ("\rLoop took {:.2}s on {}s ".format(end - start, SAMPLING_FREQUENCY))
debug ("\rLoop took {:.2}s on {}s ".format(end - start, SAMPLING_FREQUENCY))
return (in_data, pyaudio.paContinue)

View File

@ -1,2 +1,3 @@
librosa==0.6.1
librosa==0.7.2
redis
statistics