Skip to content

Commit

Permalink
adjusted interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
lynerist committed May 6, 2021
1 parent 6eeb03f commit 65c8998
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
13 changes: 10 additions & 3 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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")

Expand Down
29 changes: 16 additions & 13 deletions sintesi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
from functions import *

song = "evangelion"
instrument = "trumpet"
instrument = "violin"

try:
instrument = Instrument(instrument)
instrument = Instrument(instrument)
except FileNotFoundError as e :
print(e)
exit()

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)

# 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()

Expand All @@ -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)
Expand Down

0 comments on commit 65c8998

Please sign in to comment.