import os
import regex as re
import subprocess
import urllib
import numpy as np
import tensorflow as tf
from IPython.display import Audio
cwd = os.path.dirname(__file__)
def load_training_data():
with open(os.path.join(cwd, "irish.abc"), "r") as f:
text = f.read()
songs = extract_song_snippet(text)
return songs
load_training_data()
def extract_song_snippet(text):
pattern = '(^|\n\n)(.*?)\n\n'
search_results = re.findall(pattern, text, overlapped=True, flags=re.DOTALL)
songs = [song[1] for song in search_results]
print("Found {} songs in text".format(len(songs)))
return songs
def save_song_to_abc(song, filename="tmp"):
save_name = "{}.abc".format(filename)
with open(save_name, "w") as f:
f.write(song)
return filename
def abc2wav(abc_file):
path_to_tool = os.path.join(cwd, 'bin', 'abc2wav')
cmd = "{} {}".format(path_to_tool, abc_file)
return os.system(cmd)
def play_wav(wav_file):
return Audio(wav_file)
def play_song(song):
basename = save_song_to_abc(song)
ret = abc2wav(basename+'.abc')
if ret == 0: #did not suceed
return play_wav(basename+'.wav')
return None
def play_generated_song(generated_text):
songs = extract_song_snippet(generated_text)
if len(songs) == 0:
print("No valid songs found in generated text. Try training the \
model longer or increasing the amount of generated music to \
ensure complete songs are generated!")
for song in songs:
play_song(song)
print("None of the songs were valid, try training longer to improve \
syntax.")
def test_batch_func_types(func, args):
ret = func(*args)
assert len(ret) == 2, "[FAIL] get_batch must return two arguments (input and label)"
assert type(ret[0]) == np.ndarray, "[FAIL] test_batch_func_types: x is not np.array"
assert type(ret[1]) == np.ndarray, "[FAIL] test_batch_func_types: y is not np.array"
print("[PASS] test_batch_func_types")
return True
def test_batch_func_shapes(func, args):
dataset, seq_length, batch_size = args
x, y = func(*args)
correct = (batch_size, seq_length)
assert x.shape == correct, "[FAIL] test_batch_func_shapes: x {} is not correct shape {}".format(x.shape, correct)
assert y.shape == correct, "[FAIL] test_batch_func_shapes: y {} is not correct shape {}".format(y.shape, correct)
print("[PASS] test_batch_func_shapes")
return True
def test_batch_func_next_step(func, args):
x, y = func(*args)
assert (x[:,1:] == y[:,:-1]).all(), "[FAIL] test_batch_func_next_step: x_{t} must equal y_{t-1} for all t"
print("[PASS] test_batch_func_next_step")
return True
def test_custom_dense_layer_output(y):
true_y = np.array([[0.2697859, 0.45750418, 0.66536945]],dtype='float32')
assert tf.shape(y).numpy().tolist() == list(true_y.shape), "[FAIL] output is of incorrect shape. expected {} but got {}".format(true_y.shape, y.numpy().shape)
np.testing.assert_almost_equal(y.numpy(), true_y, decimal=7, err_msg="[FAIL] output is of incorrect value. expected {} but got {}".format(y.numpy(), true_y), verbose=True)
print("[PASS] test_custom_dense_layer_output")
return True
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
最新资源
- Comsol粗糙单裂隙渗流传热耦合数值模型, 细模型边界条件以及模型建立
- 基于支持向量机的语音情感识别MATLAB代码
- 【天线】基于matlab时域差分FDTD方法喇叭天线仿真(绘制电场方向图)【含Matlab源码 9703期】.zip
- 【飞行器】基于matlab ode45飞行器姿态控制仿真【含Matlab源码 8869期】.mp4
- 【语音加密】基于matlab GUI语音信号加密解密【含Matlab源码 295期】.mp4
- 【水声通信】基于matlab水中声纳模型仿真【含Matlab源码 9719期】.zip
- 【数字信号去噪】基于matlab ANC算法多通道主动噪声控制【含Matlab源码 9963期】.zip
- 【OFDM仿真】基于matlab CP-OFDM传输链路仿真【含Matlab源码 10012期】.zip
- 弱小目标检测20250107
- 裂隙岩体热-流-固耦合数值建模
- 毕业设计基于机器学习的DDoS入侵检测python源码+文档说明(高分项目)
- BMS仿真电池平衡控制策略仿真similink 动力电池管理系统仿真 BMS + Battery Simulink 控制策略模型, 动力电池物理模型,需求说明文档 BMS算法模型包含状态切模型、S
- MySQL配置文件my.ini
- win32汇编环境,对话框程序画扇形与饼形
- 社交推理游戏中的大型语言模型评估框架-狼人杀竞技场(Werewolf Arena)的研究与应用
- 基于SpringBoot的山西文旅网((源码+数据库+论文+ppt+包调试+一对一指导)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈