Esempio n. 1
0
 def sortFiles(self):
     # Get list of wav files, recursively
     fileList = utility.getFiles(self.snippetDirectory, '.wav')
     # Array of per-file, per-note tonalities.  Each file gets a score for each pitch
     tonalities = np.zeros((len(fileList), self.nNotes))
     print "Getting tonalities..."
     for n in np.arange(len(fileList)):
         # Get audio data
         audioData, fs = utility.getWavData(fileList[n])
         # Make sure the sampling rate matches...
         assert fs == self.fs
         # Get the tonality scores for all notes for this file
         tonalities[n] = self.getTonality(audioData)
     # Hash for the fundamental frequencies of each file
     fileFrequencies = {}
     # Find the best file for each note
     print "Finding best candidates for notes"
     for n in np.arange(self.nNotes):
         # Get the tonality scores
         tonalitiesForThisNote = tonalities[:, n]
         # Get the sorted indices of tonality scores
         tonalitiesSort = np.argsort(tonalitiesForThisNote)[::-1]
         # Keep track of which sorted array index we're getting the frequency for
         sortedIndex = 0
         # What frequency is the note we're looking for?
         targetHz = utility.midiToHz(self.baseNote + n)
         # What's the detected Hz of the note?
         detectedHz = 1.0
         # Until we find an audio file whose YIN detected pitch is sufficiently close
         while (
             (targetHz / detectedHz) <
             (1 - PITCH_TOLERANCE) or (targetHz / detectedHz) >
             (1 + PITCH_TOLERANCE)) and sortedIndex < tonalitiesSort.shape[
                 0] and sortedIndex < PITCHES_TO_RUN:
             # If this file has not been YIN analyzed yet, analyze it
             if not fileFrequencies.has_key(tonalitiesSort[sortedIndex]):
                 audioData, fs = utility.getWavData(
                     fileList[tonalitiesSort[sortedIndex]])
                 # ... and store it so that you don't have to calculate it next time
                 fileFrequencies[tonalitiesSort[
                     sortedIndex]] = self.yinPitchDetect(audioData)
             detectedHz = fileFrequencies[tonalitiesSort[sortedIndex]]
             # Check the next file next time
             sortedIndex += 1
         # If we didn't run out of files, copy out the file that we found (with the closest pitch)
         if sortedIndex < len(fileList):
             shutil.copy(
                 fileList[tonalitiesSort[sortedIndex - 1]],
                 os.path.join(self.destinationDirectory,
                              str(n + self.baseNote) + ".wav"))
         else:
             shutil.copy(
                 fileList[tonalitiesSort[0]],
                 os.path.join(self.destinationDirectory,
                              str(n + self.baseNote) + ".wav"))
 def sortFiles( self ):
   # Get list of wav files, recursively
   fileList = utility.getFiles( self.snippetDirectory, '.wav' )
   # Array of per-file, per-note tonalities.  Each file gets a score for each pitch
   tonalities = np.zeros( ( len( fileList ), self.nNotes ) )
   print "Getting tonalities..."
   for n in np.arange( len( fileList ) ):      
     # Get audio data
     audioData, fs = utility.getWavData( fileList[n] )
     # Make sure the sampling rate matches...
     assert fs == self.fs
     # Get the tonality scores for all notes for this file
     tonalities[n] = self.getTonality( audioData )
   # Hash for the fundamental frequencies of each file
   fileFrequencies = {}
   # Find the best file for each note
   print "Finding best candidates for notes"
   for n in np.arange( self.nNotes ):
     # Get the tonality scores
     tonalitiesForThisNote = tonalities[:,n]
     # Get the sorted indices of tonality scores
     tonalitiesSort = np.argsort( tonalitiesForThisNote )[::-1]
     # Keep track of which sorted array index we're getting the frequency for
     sortedIndex = 0
     # What frequency is the note we're looking for?
     targetHz = utility.midiToHz( self.baseNote + n )
     # What's the detected Hz of the note?
     detectedHz = 1.0
     # Until we find an audio file whose YIN detected pitch is sufficiently close
     while ((targetHz/detectedHz) < (1 - PITCH_TOLERANCE) or (targetHz/detectedHz) > (1 + PITCH_TOLERANCE)) and sortedIndex < tonalitiesSort.shape[0] and sortedIndex < PITCHES_TO_RUN:
       # If this file has not been YIN analyzed yet, analyze it
       if not fileFrequencies.has_key( tonalitiesSort[sortedIndex] ):        
         audioData, fs = utility.getWavData( fileList[tonalitiesSort[sortedIndex]] )
         # ... and store it so that you don't have to calculate it next time
         fileFrequencies[tonalitiesSort[sortedIndex]] = self.yinPitchDetect( audioData )
       detectedHz = fileFrequencies[tonalitiesSort[sortedIndex]]
       # Check the next file next time
       sortedIndex += 1
     # If we didn't run out of files, copy out the file that we found (with the closest pitch)
     if sortedIndex < len( fileList ):
       shutil.copy( fileList[tonalitiesSort[sortedIndex-1]], os.path.join( self.destinationDirectory, str(n + self.baseNote) + ".wav" ) )
     else:
       shutil.copy( fileList[tonalitiesSort[0]], os.path.join( self.destinationDirectory, str(n + self.baseNote) + ".wav" ) )
# batchCreateMIDITestFiles.py
# Given some MIDI files, mes
#
# Created by Colin Raffel on 10/7/10

import utility
import createMIDITestFiles
import numpy as np
import sys
import os

if __name__ == "__main__":
  if len(sys.argv) < 3:
    print "Usage: %s datasetDirectory outputDirectory" % sys.argv[0]
    sys.exit(-1)

  files = utility.getFiles( sys.argv[1], '.mid' )

  for file in files:
    filename = os.path.split( os.path.splitext( file )[0] )[1]
    outputDirectory = os.path.join( sys.argv[2], filename )
    os.makedirs( outputDirectory )
    filesWritten = createMIDITestFiles.MIDIToTestFiles( file ).createMIDITestFiles( outputDirectory, 10, 20, np.arange(0, 100, 10) )
    if len(filesWritten) is 0:
      os.rmdir( outputDirectory )