-
Notifications
You must be signed in to change notification settings - Fork 1
/
melody.py
85 lines (73 loc) · 2.78 KB
/
melody.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
import os
import io
import time
import traceback
import logging
import ctc_utils
import numpy as np
import simpleaudio as sa
from pathlib import Path
from midi.player import *
from scipy.io.wavfile import write as WAV
import tensorflow.python.util.deprecation as deprecation
def generateWAV(all_predictions, merged):
logging.basicConfig(filename='logs/melody.log', level=logging.DEBUG)
SEMANTIC = ''
playlist = []
track = 0
export = 0
single = merged
directory = 'data\\melody\\'
del_directory = '\\data\\melody'
mypath = Path().absolute()
delete_str = str(mypath) + del_directory
remove_dir = os.listdir(delete_str)
for item in remove_dir:
if (item.endswith(".wav")) or (item.endswith(".txt")):
os.remove(os.path.join(delete_str, item))
all_predictions = [x for x in all_predictions if x.strip()]
logging.info("SYMBOL: printing all predictions")
logging.info(all_predictions)
# all_txt = ''.join(map(str, all_predictions))
# with open(directory + 'all_predictions'+'.txt', 'w') as file:
# file.write(all_txt)
try:
for SEMANTIC in all_predictions:
if SEMANTIC:
# gets the audio file
audio = get_sinewave_audio(SEMANTIC)
# horizontally stacks the freqs
audio = np.hstack(audio)
# normalizes the freqs
audio *= 32767 / np.max(np.abs(audio))
#converts it to 16 bits
audio = audio.astype(np.int16)
playlist.append(audio)
with open(directory + 'predictions'+ str(export) +'.txt', 'w') as file:
file.write(SEMANTIC)
export+=1
except Exception as e:
logging.error(traceback.format_exc())
logging.error("AUDIO: could not generate sinewave audio")
try:
if(playlist):
if(single == "true"):
for song in playlist:
output_file = directory + 'staff' + str(track) + '.wav'
WAV(output_file, 44100, song)
track+=1
else:
output_file = directory + "full_song" + '.wav'
full_song = None
for song in playlist:
small_file = directory + 'staff' + str(track) + '.wav'
WAV(small_file, 44100, song)
track+=1
if (full_song) is None:
full_song = song
else:
full_song = np.concatenate((full_song, song))
WAV(output_file, 44100, full_song)
except Exception as e:
logging.error(traceback.format_exc())
logging.error("AUDIO: could not generate sinewave audio")