Skip to content

Commit

Permalink
Python3 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
carthach committed Mar 11, 2018
1 parent 24680d3 commit 185d0ad
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions PyConcat/Classifier.py
@@ -0,0 +1,15 @@
from sklearn.neighbors import KNeighborsClassifier

class Classifier:

def __init__(self):
pass

def trainClassifier(self):
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)

def classifyInstance(self):
pass


61 changes: 44 additions & 17 deletions PyConcat/Extractor.py
Expand Up @@ -8,6 +8,8 @@
import fnmatch
import os
import madmom
import librosa
from madmom.audio.filters import LogarithmicFilterbank
from multiprocessing import Process, Queue
from multiprocessing import Pool

Expand Down Expand Up @@ -167,8 +169,11 @@ def loadAudio(self, filename):

#Essentia's loader (above) has a bug that doesn't close files
#It causes problems processing large number of files, use madmom instead
audio, sample_rate = madmom.audio.signal.load_wave_file(filename, num_channels=1)
audio = essentia.array(audio)
# audio, sample_rate = madmom.audio.signal.load_wave_file(filename, num_channels=1)

y, sr = librosa.load(filename, sr=None)

audio = essentia.array(y)

return audio

Expand All @@ -182,6 +187,7 @@ def getListOfFiles(self, path, filterPattern=""):
"""
files = []

#For the recursive functionality we need a list of paths
if type(path) is not list:
path = [path]

Expand Down Expand Up @@ -235,7 +241,7 @@ def extractBeats(self, fileName):
return ticks, slices, fileName

def extractOnsetsMadmom(self, filename, method="CNN"):
"""Use an onset detector to return beat locations
"""Use Madmom's Superflux or CNN detector to return onset locations
:param fileName: the file to load and extract beats from
Expand All @@ -247,25 +253,49 @@ def extractOnsetsMadmom(self, filename, method="CNN"):
fileName: pass out the filename again
"""
act = None
onsetFunctionExtractor = None
peakPicker = None

#Choose an onset detection function
if "CNN" in method:
act = madmom.features.onsets.CNNOnsetProcessor()
onsetFunctionExtractor = madmom.features.onsets.CNNOnsetProcessor(fps=100)
peakPicker = madmom.features.onsets.OnsetPeakPickingProcessor(threshold=0.54, smooth=0.05)
else:
act = madmom.features.onsets.SpectralOnsetProcessor(onset_method='superflux', fps=200,
filterbank = madmom.audio.filters.LogarithmicFilterbank,
num_bands = 24, log = np.log10)
#get the onsets
onsetFunction = act(filename)
# onsetFunctionExtractor = madmom.features.onsets.SpectralOnsetProcessor(onset_method='superflux',
# filterbank = LogarithmicFilterbank,
# num_bands = 24, log = np.log10,
# fmin=30, fmax=17000,
# mul=1, add=1,
# diff_ratio=0.5,
# diff_max_bins=3,
# positive_diffs=True
# )
# peakPicker = madmom.features.onsets.OnsetPeakPickingProcessor(threshold=1.1, pre_max=0.01,
# post_max=0.05, pre_avg=0.15,
# post_avg=0, combine=0.03, delay=0)

onsetFunctionExtractor = madmom.features.onsets.SpectralOnsetProcessor(onset_method='superflux',
filterbank=LogarithmicFilterbank,
num_bands=24, log=np.log10,
fmin=27.5, fmax=16000,
mul=1, add=1,
diff_ratio=0.5,
diff_max_bins=3,
positive_diffs=True
)
#Old SupeFlux Params
peakPicker = madmom.features.onsets.OnsetPeakPickingProcessor(threshold=1.25, pre_max=0.03,
post_max=0.03, pre_avg=0.10,
post_avg=0.07, combine=0.03, delay=0)

proc = madmom.features.onsets.OnsetPeakPickingProcessor(fps=100)
onsetTimes = proc(onsetFunction)
#get the onsets
onsetFunction = onsetFunctionExtractor(filename)
onsetTimes = peakPicker(onsetFunction)

return onsetTimes

def extractOnsetsEssentia(self,filename):
"""Use an onset detector to return beat locations
"""Use Essentia's Onset Rate onset detector to return beat locations
:param fileName: the file to load and extract beats from
Expand All @@ -281,12 +311,9 @@ def extractOnsetsEssentia(self,filename):
onsetTimes = None

onsetRate = essentia.standard.OnsetRate()
duration = essentia.standard.Duration()

audio = self.loadAudio(filename)
onsetRateResult = onsetRate(audio)

onsetTimes, onsetRate = onsetRateResult
onsetTimes, onsetRate = onsetRate(audio)

return onsetTimes

Expand Down
16 changes: 8 additions & 8 deletions PyConcat/UnitSelection.py
@@ -1,7 +1,7 @@
import numpy as np
import HMM
import kBestViterbi.kBestViterbi as kb
import kBestViterbi.networkx_viterbi as kbg
from .HMM import HMM
from .kBestViterbi import kBestViterbi as kb
from .kBestViterbi import networkx_viterbi as kbg
from scipy.spatial import distance
from sklearn import preprocessing
from time import time
Expand Down Expand Up @@ -189,7 +189,7 @@ def computeDistanceWithWeights(targetFeatures, corpusFeatures):

return a, b

def unitSelection(targetFeatures, corpusFeatures, method="kdtree", normalise="MinMax", topK=30):
def unitSelection(targetFeatures, corpusFeatures, method="kdtree", normalise="MinMax", topK=10):
"""Optionally normalise and use one of the methods to return a sequence of indices
:param targetFeatures:
Expand All @@ -204,7 +204,7 @@ def unitSelection(targetFeatures, corpusFeatures, method="kdtree", normalise="Mi
:return: the sequence path(s)
"""
print " Scaling and weighting feature vectors..."
print(" Scaling and weighting feature vectors...")

scalar = None

Expand All @@ -229,12 +229,12 @@ def unitSelection(targetFeatures, corpusFeatures, method="kdtree", normalise="Mi
corpusFeatures = combinedFeatures[len(targetFeatures):, :]

#Call this method to compute the weighted a/b matrices for HMM
print " Computing distance matrices..."
print(" Computing distance matrices...")
a, b = computeDistanceWithWeights(targetFeatures, corpusFeatures)

print " Performing unit selection..."
print(" Performing unit selection...")
targetCostWeight = 1.0
concatCostWeight = 1.0
concatCostWeight = 0.0

#Return the results of the chosen unit selection
if method == "kdTree":
Expand Down
4 changes: 2 additions & 2 deletions PyConcat/kBestViterbi/kBestViterbi.py
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd
import networkx as nx
import networkx_viterbi as nxv
from . import networkx_viterbi as nxv
import heapq
import itertools

Expand Down Expand Up @@ -34,7 +34,7 @@ def exhaustiveWithCosts(a, b):

scores.append((score, ss))

print ss
print(ss)

return sorted(scores)

Expand Down
4 changes: 2 additions & 2 deletions PyConcat/kBestViterbi/networkx_viterbi.py
@@ -1,6 +1,6 @@
import networkx as nx
import numpy as np
from simple_paths_with_costs import * #Our custom simple_paths
from . simple_paths_with_costs import * #Our custom simple_paths
import heapq
import itertools

Expand Down Expand Up @@ -295,6 +295,6 @@ def kViterbiGraphWithCosts(a, b, topK, weights=(1.0, 1.0)):

t1 = time()

print 'function ITSELF takes %f' % (t1 - t0)
print('function ITSELF takes %f' % (t1 - t0))

return paths
2 changes: 1 addition & 1 deletion config.yaml
@@ -1,5 +1,5 @@
timeScale : 'onsets'
writeOnsets : False
unitSelection : 'linearSearch'
unitSelection : 'kdTree'
normalisation : 'MinMax'
stretchUnits : True
9 changes: 6 additions & 3 deletions main.py
Expand Up @@ -92,7 +92,7 @@ def parser(lgd=False, threshold=1.1):
args = p.parse_args()
# print arguments
if args.verbose:
print args
print(args)
# return args

#Override the command line arguments with YAML
Expand Down Expand Up @@ -126,14 +126,17 @@ def main(args):
targetFilename, corpusPath = getCorpus(args.input)

#Get list of corpus files
corpusFilenames = extractor.getListOfWavFiles(corpusPath)
corpusFilenames = extractor.getListOfFiles(corpusPath, "*.mp3")

print(corpusPath)

#Segment and extract features
print("Extracting Target")
targetFeatures, targetUnits, targetUnitTimes = extractor.analyseFile(targetFilename, writeOnsets, scale=timeScale)
print("Extracting Corpus")
corpusFeatures, corpusUnits, corpusUnitTimes = extractor.analyseFiles(corpusFilenames, writeOnsets, scale=timeScale, yamlOutputFolder=outputPath)


# # For graphing
# costMatrix = computeDistanceMatrix(targetFeatures, targetFeatures)
#
Expand Down Expand Up @@ -171,7 +174,7 @@ def main(args):
#Optionally plot data
#plotData(sequence, targetFeatures, corpusFeatures)

print "done"
print("done")

def readYAMLConfig(filename, args):
"""
Expand Down

0 comments on commit 185d0ad

Please sign in to comment.