[fix] update for librosa 0.7.2
This commit is contained in:
parent
678aae0c94
commit
7f42b10b95
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.*.swp
|
83
redilysis.py
83
redilysis.py
@ -11,10 +11,12 @@ from __future__ import print_function
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import librosa
|
import librosa
|
||||||
|
import math
|
||||||
import numpy
|
import numpy
|
||||||
import os
|
import os
|
||||||
import pyaudio
|
import pyaudio
|
||||||
import redis
|
import redis
|
||||||
|
import statistics
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -25,9 +27,9 @@ def debug(*args, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
# Define default variables.
|
# Define default variables.
|
||||||
_BAND_RANGE = 7
|
BAND_OCTAVES = 10 # 12 * 9 octaves
|
||||||
|
_BAND_TONES = BAND_OCTAVES * 12 # octaves * notes per octave
|
||||||
_CHANNELS = 1
|
_CHANNELS = 1
|
||||||
_ENERGY_THRESHOLD = 0.1
|
|
||||||
_FRAMES_PER_BUFFER = 4410
|
_FRAMES_PER_BUFFER = 4410
|
||||||
_N_FFT = 4096
|
_N_FFT = 4096
|
||||||
_RATE = 44100
|
_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('--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('--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('--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
|
# Redis Args
|
||||||
parser.add_argument("-i","--ip",help="IP address of the Redis server ",default="127.0.0.1",type=str)
|
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)
|
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")
|
parser.add_argument("-v","--verbose",action="store_true",help="Verbose")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# global
|
||||||
|
bpm = 120.0
|
||||||
|
|
||||||
# Set real variables
|
# 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
|
CHANNELS = args.channels
|
||||||
DEVICE = args.device
|
DEVICE = args.device
|
||||||
ENERGY_THRESHOLD = args.energy_threshold
|
|
||||||
FRAMES_PER_BUFFER = int(args.rate * args.sampling_frequency )
|
FRAMES_PER_BUFFER = int(args.rate * args.sampling_frequency )
|
||||||
LIST_DEVICES = args.list_devices
|
LIST_DEVICES = args.list_devices
|
||||||
MODE = args.mode
|
MODE = args.mode
|
||||||
@ -66,17 +71,11 @@ ip = args.ip
|
|||||||
port = args.port
|
port = args.port
|
||||||
verbose = args.verbose
|
verbose = args.verbose
|
||||||
|
|
||||||
debug( "frames", FRAMES_PER_BUFFER)
|
if( MODE == "bpm" and SAMPLING_FREQUENCY < 0.5 ):
|
||||||
if( MODE == "bpm" and RATE < 0.5 ):
|
debug( "You should use a --sampling_frequency superior to 0.5 in BPM mode...")
|
||||||
debug( "You should use a --rate superior to 0.5 in BPM mode...")
|
|
||||||
|
|
||||||
|
|
||||||
|
melFilter = librosa.filters.mel(RATE, N_FFT, BAND_TONES, fmin=F_LO, fmax=F_HI)
|
||||||
# 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)
|
|
||||||
|
|
||||||
|
|
||||||
r = redis.Redis(
|
r = redis.Redis(
|
||||||
host=ip,
|
host=ip,
|
||||||
@ -90,12 +89,12 @@ def list_devices():
|
|||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
i = 0
|
i = 0
|
||||||
n = p.get_device_count()
|
n = p.get_device_count()
|
||||||
debug("\nFound {} devices\n".format(n))
|
print("\nFound {} devices\n".format(n))
|
||||||
debug (" {} {}".format('ID', 'Device name'))
|
print(" {} {}".format('ID', 'Device name'))
|
||||||
while i < n:
|
while i < n:
|
||||||
dev = p.get_device_info_by_index(i)
|
dev = p.get_device_info_by_index(i)
|
||||||
if dev['maxInputChannels'] > 0:
|
if dev['maxInputChannels'] > 0:
|
||||||
debug (" {} {}".format(i, dev['name']))
|
print(" {} {}".format(i, dev['name']))
|
||||||
i += 1
|
i += 1
|
||||||
if( LIST_DEVICES ):
|
if( LIST_DEVICES ):
|
||||||
list_devices()
|
list_devices()
|
||||||
@ -103,8 +102,6 @@ if( LIST_DEVICES ):
|
|||||||
|
|
||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
|
|
||||||
# global
|
|
||||||
bpm = 120.0
|
|
||||||
|
|
||||||
def m_bpm(audio_data):
|
def m_bpm(audio_data):
|
||||||
"""
|
"""
|
||||||
@ -140,9 +137,6 @@ def m_bpm(audio_data):
|
|||||||
def m_spectrum(audio_data):
|
def m_spectrum(audio_data):
|
||||||
"""
|
"""
|
||||||
This function saves fast analysis to redis
|
This function saves fast analysis to redis
|
||||||
* spectrum
|
|
||||||
* RMS
|
|
||||||
* tuning
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Compute real FFT.
|
# Compute real FFT.
|
||||||
@ -151,31 +145,34 @@ def m_spectrum(audio_data):
|
|||||||
# Compute mel spectrum.
|
# Compute mel spectrum.
|
||||||
melspectrum = melFilter.dot(abs(fft))
|
melspectrum = melFilter.dot(abs(fft))
|
||||||
|
|
||||||
# Get RMS
|
|
||||||
rms = librosa.feature.rmse( S=melspectrum, frame_length=FRAMES_PER_BUFFER )
|
|
||||||
|
|
||||||
# Initialize output characters to display.
|
# Initialize output characters to display.
|
||||||
bit_list = [0]*BAND_RANGE
|
spectrum_120 = [0]*BAND_TONES
|
||||||
count = 0
|
spectrum_10 = [0]*BAND_OCTAVES
|
||||||
highest_index = -1
|
spectrum_oct = [[] for i in range(10)]
|
||||||
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
|
|
||||||
|
|
||||||
# If there is energy in this frequency, mark it
|
# Assign values
|
||||||
if val > ENERGY_THRESHOLD:
|
for i in range(BAND_TONES):
|
||||||
count += 1
|
val = round(melspectrum[i],2)
|
||||||
bit_list[i] = val
|
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
|
# Save to redis
|
||||||
debug( 'rms:{} bit_list:{} highest_index:{}'.format(rms , bit_list, highest_index ))
|
#debug( 'spectrum_120:{} '.format(spectrum_120))
|
||||||
r.set( 'rms', "{}".format(rms.tolist()) )
|
#debug( 'spectrum_10:{}'.format(spectrum_10))
|
||||||
r.set( 'spectrum', json.dumps( bit_list ) )
|
#debug( 'rms:{}'.format(rms_avg))
|
||||||
r.set( 'tuning', highest_index )
|
r.set( 'spectrum_120', json.dumps( spectrum_120 ) )
|
||||||
|
r.set( 'spectrum_10', json.dumps( spectrum_10 ) )
|
||||||
|
r.set( 'rms', "{}".format(rms_avg) )
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -191,7 +188,7 @@ def callback(in_data, frame_count, time_info, status):
|
|||||||
debug( "Unknown mode. Exiting")
|
debug( "Unknown mode. Exiting")
|
||||||
os._exit(2)
|
os._exit(2)
|
||||||
end = time.time()
|
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)
|
return (in_data, pyaudio.paContinue)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
librosa==0.6.1
|
librosa==0.7.2
|
||||||
redis
|
redis
|
||||||
|
statistics
|
||||||
|
Loading…
Reference in New Issue
Block a user