From 65c8998bc5b80b8ad0e076dbda87d55a52c95247 Mon Sep 17 00:00:00 2001 From: lynerist Date: Thu, 6 May 2021 11:54:08 +0200 Subject: [PATCH] adjusted interpolation --- functions.py | 13 ++++++++++--- sintesi.py | 29 ++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/functions.py b/functions.py index a857a47..b1aec58 100644 --- a/functions.py +++ b/functions.py @@ -9,12 +9,18 @@ def pitchChange(sound, change): adjuste the pitch by change semitones """ semitoneDistance = 2 ** (1/12) + #numpy interp1 + speed = (semitoneDistance**change) newAudio = sound._spawn(sound.raw_data, overrides={"frame_rate": int(sound.frame_rate * speed)}) return newAudio.set_frame_rate(sound.frame_rate) def adjustLength(sound:AudioSegment, durationDesired): + #loop acceso spento + #ping pong + #attacco 12/15 ms + #rilascio 30/40 ms + fade out """ Adjust from the center cutting the samples or looping them """ @@ -35,7 +41,7 @@ def adjustLength(sound:AudioSegment, durationDesired): #first half adjusted = sound[:half-toTrim/2] #second half - adjusted = adjusted.append(sound[half+toTrim/2:], crossfade) + adjusted = adjusted.append(sound[half+int(toTrim/2):], crossfade) else: toAdd = (durationDesired - sound.duration_seconds) * 1000 @@ -115,7 +121,7 @@ def __str__ (self): class Note: def __init__ (self, note, countTicks, velocity): - self.note = note + self.note = int(note) self.startTime = countTicks self.velocity = velocity @@ -133,7 +139,8 @@ def __init__(self, instrument): rangeFile = open(self.path + "range.txt", "r") self.rangeNotes = tuple(rangeFile.readline().split()) self.extension = rangeFile.readline() - self.notes = os.listdir(self.path) + self.notes = [int(x[:-4]) for x in os.listdir(self.path) if len(x) < 8] + self.notes.sort() except FileNotFoundError : raise FileNotFoundError("Range file missing in instrument directory!\n") diff --git a/sintesi.py b/sintesi.py index f0f795d..d8df0b8 100644 --- a/sintesi.py +++ b/sintesi.py @@ -2,10 +2,10 @@ from functions import * song = "evangelion" -instrument = "trumpet" +instrument = "violin" try: - instrument = Instrument(instrument) + instrument = Instrument(instrument) except FileNotFoundError as e : print(e) exit() @@ -13,6 +13,7 @@ midi = MidiInterface(mido.MidiFile(f"midi/{song}.mid", clip=True)) duration = midi.duration +#TODO essentia / madmom # genero un file audio vuoto lungo quanto il file midi (+500 per non troncare alla fine) audio = AudioSegment.silent(midi.length*1000 + 500) @@ -20,6 +21,9 @@ # il clock nella traccia countTicks = 0 +#TODO note ON DA SISTEMARE (nel file convertjustnoteon) +#TODO note off stoppa ogni nota + # Dizionario per salvarmi il tick di inizio dei note on per poi associarli ai note off relativi noteOnCollection = dict() @@ -45,23 +49,22 @@ if note.note in cacheAudioSamples.keys(): audioNote = cacheAudioSamples[note.note] else: - #controllo se sono fuori range (Da sistemare) - if int(msg.note) < int(instrument.rangeNotes[0]) or int(msg.note) > int(instrument.rangeNotes[1]): - audioNote = AudioSegment.from_file(instrument.path + instrument.rangeNotes[int(msg.note) > int(instrument.rangeNotes[1])] + "." + instrument.extension) - audioNote = pitchChange(audioNote, (int(msg.note) - int(instrument.rangeNotes[int(msg.note) > int(instrument.rangeNotes[1])]))) - else: - offset = 0 - while not "%d.%s"%(note.note - offset, instrument.extension) in instrument.notes: - offset += 1 - audioNote = AudioSegment.from_file("%s%d.%s"%(instrument.path, note.note - offset, instrument.extension)) - audioNote = pitchChange(audioNote, offset) + minDistance = 127 + minDistanceNote = 0 + for sampleNote in instrument.notes: + distance = abs(sampleNote - note.note) + if distance < minDistance: + minDistance, minDistanceNote = distance, sampleNote + else: + break + audioNote = AudioSegment.from_file(f"{instrument.path}{minDistanceNote}.{instrument.extension}") + audioNote = pitchChange(audioNote, note.note - minDistanceNote) cacheAudioSamples[note.note] = audioNote #calcolato sulla velocity รจ un valore negativo nel range [-18, 0] che viene usato per applicare una riduzione massima di 18dB volume = (note.velocity/127)*18 - 18 - #durata della nota midi noteLength = duration(countTicks-note.startTime) #scrivo il campione (prima adatto la durata del campione)