<filename>chordgram.py<gh_stars>0
from chromagram1 import *
import librosa
import librosa.display
import numpy as np
from scipy.stats import mode

offset = 0
	# duration: only load up to this much audio (in seconds)
duration = 50

	## chromagram
	# sr: sampling rate
sr = 44100
	# hop_length: number of samples between successive chroma frames (frame size)
hop_length = 4096

	## chordgram
	# w: filter size; w = 30 to be a good comromise that works well for most songs
w = 30

#file = "/Users/meenakshi/Music/Hotel Californiakaraoke.wav"
def generate_template():
	template = {}
	majors = ["C","D","D","E","E","F","F","G","A","A","B","B"]
	minors = ["c","d","d","e","e","f","f","g","a","a","b","b"]

	# template for C and Cm
	tc = [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
	tcm = [1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]
	shifted = 0

	for chord in majors:
		template[chord] = tc[12 - shifted:] + tc[:12 - shifted]
		shifted += 1

	for chord in minors:
		template[chord] = tcm[12 - shifted:] + tcm[:12 - shifted]
		shifted += 1

	# template for no chords
	tnc = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	template["NC"] = tnc

	return template

def cossim(u, v):
	"""
	:param u: non-negative vector u
	:param v: non-negative vector v
	:return: the cosine similarity between u and v
	"""
	return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))

def chordgram(C, display=False):
	"""
	:param C: chromagram C
	:return: chordgram H
	"""
	frames = C.shape[1]

	# initialize
	template = generate_template()
	chords = list(template.keys())
	chroma_vectors = np.transpose(C)
	# chordgram
	H = []

	for n in np.arange(frames):
		cr = chroma_vectors[n]
		sims = []

		for chord in chords:
			t = template[chord]
			# calculate cos sim, add weight
			if chord == "NC":
				sim = cossim(cr, t) * 0.7
			else:
				sim = cossim(cr, t)
			sims += [sim]
		H += [sims]
	H = np.transpose(H)

	if display == True:
		plt.figure(figsize=(10, 5))
		librosa.display.specshow(H, sr=sr, x_axis="frames")
		plt.title("Chordgram")
		plt.colorbar()
		plt.tight_layout()
		plt.show()

	return H

def smoothing(s):
	"""
	:param s: sequence s
	:return: mode filter for sequence s
	"""
	w = 15
	news = [0] * len(s)
	for k in np.arange(w, len(s) - w):
		m = mode([s[i] for i in range(k - w // 2, k + w // 2 + 1)])[0][0]
		news[k] = m
	return news

def smoothed_chordgram(H, display=False):
	"""
	:param H: chordgram
	:return: chordgram after filtering
	"""
	chords = H.shape[0]
	H1 = []

	for n in np.arange(chords):
		H1 += [smoothing(H[n])]

	H1 = np.array(H1)
	if display == True:
		plt.figure(figsize=(10, 5))
		librosa.display.specshow(H1, sr=sr, x_axis="frames")
		plt.title("Chordgram")
		plt.colorbar()
		plt.tight_layout()
		plt.show()

	return H1

def chord_sequence(H):
	"""
	:param H: chordgram H
	:return: a sequence of chords
	"""
	template = generate_template()
	chords = list(template.keys())

	frames = H.shape[1]
	H = np.transpose(H)
	R = []

	for n in np.arange(frames):
		index = np.argmax(H[n])
		if H[n][index] == 0.0:
			chord = "NC"
		else:
			chord = chords[index]

		R += [chord]

	return R

def tostring_chords(input):
	string = ""
	for r in input:
		if r == "NC":
			string += " -"
		else:
			string += " " + r
	return (string)

def findTempo(file):
	sr = 44100
	y, sr = librosa.load(file, sr=sr, offset=offset, duration=duration)
	onset_env = librosa.onset.onset_strength(y, sr=sr)
	tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sr)
	print (tempo)
# Chord-Recognition Done!

#C = chromagram(file)

<filename>chordgram.py<gh_stars>0
from chromagram1 import *
import librosa
import librosa.display
import numpy as np
from scipy.stats import mode

offset = 0
	# duration: only load up to this much audio (in seconds)
duration = 50

	## chromagram
	# sr: sampling rate
sr = 44100
	# hop_length: number of samples between successive chroma frames (frame size)
hop_length = 4096

	## chordgram
	# w: filter size; w = 30 to be a good comromise that works well for most songs
w = 30

#file = "/Users/meenakshi/Music/Hotel Californiakaraoke.wav"
def generate_template():
	template = {}
	majors = ["C","D","D","E","E","F","F","G","A","A","B","B"]
	minors = ["c","d","d","e","e","f","f","g","a","a","b","b"]

	# template for C and Cm
	tc = [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
	tcm = [1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]
	shifted = 0

	for chord in majors:
		template[chord] = tc[12 - shifted:] + tc[:12 - shifted]
		shifted += 1

	for chord in minors:
		template[chord] = tcm[12 - shifted:] + tcm[:12 - shifted]
		shifted += 1

	# template for no chords
	tnc = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	template["NC"] = tnc

	return template

def cossim(u, v):
	"""
	:param u: non-negative vector u
	:param v: non-negative vector v
	:return: the cosine similarity between u and v
	"""
	return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))

def chordgram(C, display=False):
	"""
	:param C: chromagram C
	:return: chordgram H
	"""
	frames = C.shape[1]

	# initialize
	template = generate_template()
	chords = list(template.keys())
	chroma_vectors = np.transpose(C)
	# chordgram
	H = []

	for n in np.arange(frames):
		cr = chroma_vectors[n]
		sims = []

		for chord in chords:
			t = template[chord]
			# calculate cos sim, add weight
			if chord == "NC":
				sim = cossim(cr, t) * 0.7
			else:
				sim = cossim(cr, t)
			sims += [sim]
		H += [sims]
	H = np.transpose(H)

	if display == True:
		plt.figure(figsize=(10, 5))
		librosa.display.specshow(H, sr=sr, x_axis="frames")
		plt.title("Chordgram")
		plt.colorbar()
		plt.tight_layout()
		plt.show()

	return H

def smoothing(s):
	"""
	:param s: sequence s
	:return: mode filter for sequence s
	"""
	w = 15
	news = [0] * len(s)
	for k in np.arange(w, len(s) - w):
		m = mode([s[i] for i in range(k - w // 2, k + w // 2 + 1)])[0][0]
		news[k] = m
	return news

def smoothed_chordgram(H, display=False):
	"""
	:param H: chordgram
	:return: chordgram after filtering
	"""
	chords = H.shape[0]
	H1 = []

	for n in np.arange(chords):
		H1 += [smoothing(H[n])]

	H1 = np.array(H1)
	if display == True:
		plt.figure(figsize=(10, 5))
		librosa.display.specshow(H1, sr=sr, x_axis="frames")
		plt.title("Chordgram")
		plt.colorbar()
		plt.tight_layout()
		plt.show()

	return H1

def chord_sequence(H):
	"""
	:param H: chordgram H
	:return: a sequence of chords
	"""
	template = generate_template()
	chords = list(template.keys())

	frames = H.shape[1]
	H = np.transpose(H)
	R = []

	for n in np.arange(frames):
		index = np.argmax(H[n])
		if H[n][index] == 0.0:
			chord = "NC"
		else:
			chord = chords[index]

		R += [chord]

	return R

def tostring_chords(input):
	string = ""
	for r in input:
		if r == "NC":
			string += " -"
		else:
			string += " " + r
	return (string)

def findTempo(file):
	sr = 44100
	y, sr = librosa.load(file, sr=sr, offset=offset, duration=duration)
	onset_env = librosa.onset.onset_strength(y, sr=sr)
	tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sr)
	print (tempo)
# Chord-Recognition Done!

#C = chromagram(file)

# duration: only load up to this much audio (in seconds) ## chromagram # sr: sampling rate # hop_length: number of samples between successive chroma frames (frame size) ## chordgram # w: filter size; w = 30 to be a good comromise that works well for most songs #file = "/Users/meenakshi/Music/Hotel Californiakaraoke.wav" # template for C and Cm # template for no chords :param u: non-negative vector u
 	:param v: non-negative vector v
 	:return: the cosine similarity between u and v :param C: chromagram C
 	:return: chordgram H # initialize # chordgram # calculate cos sim, add weight :param s: sequence s
 	:return: mode filter for sequence s :param H: chordgram
 	:return: chordgram after filtering :param H: chordgram H
 	:return: a sequence of chords # Chord-Recognition Done! #C = chromagram(file)