-
Notifications
You must be signed in to change notification settings - Fork 1
/
midiTestGen.py
83 lines (64 loc) · 2.29 KB
/
midiTestGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 18 22:19:39 2020
@author: luiz
"""
from midiutil import MIDIFile
import music21 as mus
notes = []
for iOctv in range(1,8):
for iNote in ['C', 'D', 'E', 'F', 'G', 'A', 'B']:
note = mus.pitch.Pitch(iNote)
note.octave = iOctv
notes.append(note)
max_dur = 25
min_dur = 3
def duration_decay(i):
return int(max_dur - (max_dur - min_dur)*(i/len(notes)))
dur_list = []
degrees = [n.midi for n in notes]#[60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
# inserting some extra notes manually:
degrees.insert(0,23)
degrees.insert(0,21)
track = 0
channel = 0
time = 1
pause = 2 # In beats
#duration = 1 # In beats
tempo = 60 # In BPM
vel = 16 # 0-127, as per the MIDI standard
#%% Generating midi file
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
# automatically)
MyMIDI.addTempo(track, time, tempo)
for i, pitch in enumerate(degrees):
dur_list.append(duration_decay(i))
start = 0
for d in dur_list[:i]:
start = start + d + pause
MyMIDI.addNote(track, channel, pitch, start, dur_list[-1], vel)
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
#%% Generate audio file from midi manually! Then replace the links and run this
from pydub import AudioSegment
import numpy as np
from os import path
audioPath = r"/media/luiz/Volume/Dokumente/Musik/Projekte/stwdh_major-scale_v16.wav"
outputPath = r"/media/luiz/Volume/Dokumente/Musik/Projekte/temp"
audio = AudioSegment.from_wav(audioPath)
durations_ms = np.array([])
pause_ms = pause * 60/tempo * 1000
pause_ms = pause_ms# - 1 # fine tuning, not sure why it's needed
for i, pitch in enumerate(degrees):
duration_ms = duration_decay(i) * 60/tempo * 1000
durations_ms = np.append(durations_ms, duration_ms)
for i, pitch in enumerate(degrees):
t0 = i*pause_ms + np.sum(durations_ms[:i])
t1 = i*pause_ms + np.sum(durations_ms[:i+1])
if i > 0:
t0 = t0 - 500 # start a bit earlier, to avoid cutting sound
fileName = "normal_pitch" + str(pitch) + "_v" + str(1) + ".wav"
filePath = path.join(outputPath, fileName)
audioSegment = audio[t0:t1]
audioSegment.export(filePath, format="wav")