def monophonicWavToMidi(wav_filename, midi_filename): # extract wavfile information [samplingRate, data] = scipy.io.wavfile.read(wav_filename) data = np.sum(data, 1) # average to handle cases with more than one channel onsets, durations, pitches = getSignalInformation( msignal.Signal(samplingRate, data)) # construct a midi file from the above info track = 0 channel = 0 volume = 127 tempo = 60 # 60 beats per minute or 1 beat per second midi = midiutil.MidiFile.MIDIFile(adjust_origin=None) midi.addTempo(track, 0, tempo) for i in xrange(len(onsets)): time = float(onsets[i]) / samplingRate duration = float(durations[i]) / samplingRate # Formula for MIDI pitch in the beow wiki page # https://en.wikipedia.org/wiki/MIDI_Tuning_Standard pitch = int(np.round(69 + 12 * np.log2(float(pitches[i]) / 440))) midi.addNote(track, channel, pitch, time, duration, volume) with open(midi_filename, 'wb') as output_file: midi.writeFile(output_file)
detection, threshold = data[i], thresholdFunction[i] if detection > threshold: # only accept those greater than the threshold left, right = max(0, i - radius), min(len(data), i + radius + 1) if detection == np.amax( data[left:right] ): # make sure that we have the max in a radius peakLocs.append(i) return np.array(peakLocs) if __name__ == '__main__': kiki = 'dataset/Kiki-A-Town-with-an-Ocean-View.wav' twinkle = 'dataset/twinkle twinkle little star.wav' samplingRate, data = scipy.io.wavfile.read(twinkle) data = np.add(data[:, 0], data[:, 1]) signal = msignal.Signal(samplingRate, data) sd = spectral_difference.spectralDifference(signal, default_params.WINDOW_SIZE, default_params.HOP_SIZE) sdUpsampled = scipy.signal.resample(sd, len(sd) * default_params.HOP_SIZE) thresholds = filtering.medianFilter( sd, default_params.MEDIAN_FILTER_KERNEL_SIZE, default_params.MEDIAN_FILTER_DELTA, default_params.MEDIAN_FILTER_LAMBDA) peakLocs = findPeaks(sd, thresholds, default_params.PEAK_FINDING_RADIUS) onsets = peakLocs * default_params.HOP_SIZE plt.figure(1) plt.subplot(211) plt.plot(sdUpsampled) plt.subplot(212) plt.plot(data)
def getPitch(index): return pitch_detection.getGeneralPitch(msignal.Signal(signal.samplingRate, \ signal.truncate(onsets[index], offsets[index]).data))