-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevolutionary_music.py
47 lines (37 loc) · 1.5 KB
/
evolutionary_music.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
from pydub import AudioSegment
from music_problem import MusicProblem
from notes_directory import NotesDirectory
from jmetal.operator import SPXCrossover, BinaryTournamentSelection, BitFlipMutation
from jmetal.util.observer import ProgressBarObserver, PrintObjectivesObserver
from jmetal.util.termination_criterion import StoppingByEvaluations
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm
from uniform_crossover import UniformCrossover
from uniform_mutation import UniformMutation
def generate_audio_file(notes):
notes_directory = NotesDirectory()
combined = AudioSegment.empty()
for var in notes:
combined += notes_directory.get_audio_note(var)
combined.export('data/conc.wav', format = 'wav')
target_file = 'data/songs/ode_to_joy.wav'
number_of_notes = 16
problem = MusicProblem(target_file, number_of_notes)
population_size = 100
max_evaluations = 5000
algorithm = GeneticAlgorithm(
problem = problem,
mutation = UniformMutation(probability = 1.0 / problem.number_of_notes),
crossover = UniformCrossover(1.0),
selection = BinaryTournamentSelection(),
population_size = population_size,
termination_criterion = StoppingByEvaluations(max = max_evaluations),
offspring_population_size = population_size
)
# initialize progress bar observer
algorithm.observable.register(observer = PrintObjectivesObserver())
# run algorithm and set results
algorithm.run()
front = algorithm.get_result()
# create audio file from the best result
generate_audio_file(front.variables[0])
print(front)