Skip to content

Commit 67ba5f6

Browse files
committed
Add a method to get the number of notes depending on the tempo and the desired duration
1 parent f1197a6 commit 67ba5f6

File tree

3 files changed

+84
-28
lines changed

3 files changed

+84
-28
lines changed

Diff for: .gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,11 @@ venv.bak/
113113
dmypy.json
114114

115115
# Pyre type checker
116-
.pyre/
116+
.pyre/
117+
118+
# Video File
119+
*.avi
120+
*.mp4
121+
122+
# Music File
123+
*.mid

Diff for: BeatGenerator.py

+61-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from midiutil import MIDIFile
22
import numpy as np
33
import cv2
4+
import sys
5+
import argparse
6+
import random
7+
from moviepy.video.io.VideoFileClip import VideoFileClip
48

59
def averageRGB(frame, everyNPixels):
610
"""
@@ -42,37 +46,67 @@ def averageRGBChoice(frame, colorStage, everyNPixels):
4246

4347
return averageColor
4448

45-
cap = cv2.VideoCapture('Class_Room_Tour.avi')
46-
degrees = [] # MIDI note number
47-
counter = 0
49+
def getNumberNote(musicDuration, tempo):
50+
"""
51+
Return the number of notes that the music must have to be of the specified duration
52+
musicDuration : the duration of the music (in seconds)
53+
tempo : the number of BMP of the music
54+
"""
55+
durationInMinute = musicDuration / 60
56+
return int(tempo * durationInMinute)
57+
58+
59+
60+
if __name__ == "__main__":
61+
# TODO use that to get the first argument as the filename to use
62+
# # Set an argument parser
63+
# parser = argparse.ArgumentParser(description='Convert a video file (.avi) to a music.')
64+
# parser.add_argument('filename', metavar='filename',
65+
# help='a video file')
66+
67+
# args = vars(parser.parse_args())
68+
69+
cap = cv2.VideoCapture('Class_Room_Tour.avi')
70+
degrees = [] # MIDI note number
71+
counter = 0
72+
73+
# while(cap.isOpened()):
74+
# ret, frame = cap.read()
75+
# everyNImages = 10
76+
77+
# if frame is None:
78+
# break
79+
# else:
80+
# if counter % everyNImages == 0:
81+
# degrees.append(averageRGB(frame, 100))
82+
# counter += 1
4883

49-
while(cap.isOpened()):
50-
ret, frame = cap.read()
51-
everyNImages = 10
84+
# cap.release()
85+
# cv2.destroyAllWindows()
5286

53-
if frame is None:
54-
break
55-
else:
56-
if counter % everyNImages == 0:
57-
degrees.append(averageRGB(frame, 100))
58-
counter += 1
87+
track = 0
88+
channel = 0
89+
time = 0 # In beats
90+
duration = 1 # In beats
91+
tempo = 50 # In BPM
92+
volume = 100 # 0-127, as per the MIDI standard
5993

60-
cap.release()
61-
cv2.destroyAllWindows()
94+
# Get the duration of the video
95+
clip = VideoFileClip("Class_Room_Tour.avi")
96+
print( clip.duration )
97+
# Define the number of note the music must have according to the tempo
98+
numberOfNote = getNumberNote(clip.duration, tempo)
6299

63-
track = 0
64-
channel = 0
65-
time = 0 # In beats
66-
duration = 1 # In beats
67-
tempo = 200 # In BPM
68-
volume = 100 # 0-127, as per the MIDI standard
100+
# Generate the correct number of note to have a music of the desired length
101+
for i in range(numberOfNote):
102+
degrees.append(random.randint(10, 100))
69103

70-
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
71-
# automatically)
72-
MyMIDI.addTempo(track, time, tempo)
104+
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
105+
# automatically)
106+
MyMIDI.addTempo(track, time, tempo)
73107

74-
for i, pitch in enumerate(degrees):
75-
MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
108+
for i, pitch in enumerate(degrees):
109+
MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
76110

77-
with open("major-scale.mid", "wb") as output_file:
78-
MyMIDI.writeFile(output_file)
111+
with open("major-scale.mid", "wb") as output_file:
112+
MyMIDI.writeFile(output_file)

Diff for: requirements.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
certifi==2018.11.29
2+
chardet==3.0.4
3+
decorator==4.3.2
4+
idna==2.8
5+
imageio==2.5.0
6+
imageio-ffmpeg==0.2.0
7+
MIDIUtil==1.2.1
8+
moviepy==1.0.0
9+
numpy==1.16.2
10+
opencv-python==4.0.0.21
11+
Pillow==5.4.1
12+
proglog==0.1.9
13+
requests==2.21.0
14+
tqdm==4.31.1
15+
urllib3==1.24.1

0 commit comments

Comments
 (0)