def play_from_encoder(directory):
    encoder = pickle_loader(directory + ".pkl")
    sample_rate = encoder.sample_rate
    buffer_size = 10 # sample buffer for playback. Hevent really determined what i does qualitatively. Probably affects latency
    play_duration = 100 # play duration in miliseconds
    pygame.mixer.pre_init(sample_rate, -16, 2,buffer = buffer_size) # 44.1kHz, 16-bit signed, stereo
    pygame.init()
    volLeft = 0.5;volRight=0.5 # Volume
    z_val = np.zeros([1,encoder.dimZ]) # Initial latent variable values
    port = mido.open_input(mido.get_input_names()[0]) # midi port. chooses the first midi device it detects.
    while True:
        mu_out = encoder.generateOutput(z_val)
        for msg in port.iter_pending():
            if msg.channel < z_val.shape[1]:
                z_val[0,msg.channel] = msg.value
            else:
                print "Midi channel beyond latent variables"
        mu_out = map_to_range_symmetric(mu_out,[-1,1],[-32768,32768])
        mu_out = mu_out.astype(np.int16)
        mu_out = np.array(zip(mu_out,mu_out)) # make stereo channel with same output
        sound = pygame.sndarray.make_sound(mu_out)
        channel = sound.play(-1)
        channel.set_volume(volLeft,volRight)
        pygame.time.delay(play_duration)
        sound.stop()
def play_from_encoder_no_midi(directory):
    # when no midi device is plugged in this function will play random
    # latent variable instances to inspect the capabilities of the network
    encoder = pickle_loader(directory + ".pkl")
    sample_rate = encoder.sample_rate
    buffer_size = 20
    play_duration = 100 # play duration in miliseconds
    pygame.mixer.pre_init(sample_rate, -16, 2,buffer = buffer_size) # 44.1kHz, 16-bit signed, stereo
    pygame.init()
    volLeft = 0.5;volRight=0.5 # Volume
    z_val = np.zeros([1,encoder.dimZ]) # Initial latent variable values
    while True:
        print z_val
        mu_out = encoder.generateOutput(z_val)
        mu_out = map_to_range_symmetric(mu_out,[-1,1],[-32768,32768])
        mu_out = mu_out.astype(np.int16)
        mu_out = np.array(zip(mu_out,mu_out)) # make stereo channel with same output
        sound = pygame.sndarray.make_sound(mu_out)
        channel = sound.play(-1)
        channel.set_volume(volLeft,volRight)
        pygame.time.delay(play_duration)
        sound.stop()
        z_val = np.random.uniform(-30,30,[1,encoder.dimZ])
        if z == None:
            z = np.zeros([1, self.dim_z])
        else:
            z = np.array(z)
        self.generative_z.set_value(np.float32(z))
        inp = np.zeros([z.shape[0], self.x_train.shape[1], self.x_train.shape[2], self.x_train.shape[3]])
        return np.squeeze(self.generate_from_z(np.float32(inp)))


if __name__ == "__main__":
    data_dir = parent + "/data/"
    plot_dir = current + "/plots/"
    sound_dir = current + "/sound/"
    experiment_name = "vae"

    data = pickle_loader(data_dir + "sines.pkl")  # data dictionary contains a "data" entry and a "sample rate" entry
    data = np.ndarray.astype(data, np.float32)

    data2 = data[:, 1:]
    data1 = data[:, :-1]

    dif = np.hstack([1 + np.absolute((data1 - data2) / 2), np.ones([data.shape[0], 1]).astype(np.float32)])

    # map inputs from 0 to 1
    # patch and shape for the CNN
    mean = np.mean(data)
    std = np.std(data)
    sample_rate = 880
    # data = (data -mean)/std
    data = data.reshape(data.shape[0], 1, data.shape[1], 1)
    dim_z = 5
Example #4
0
        self.generative_z.set_value(np.float32(z))
        inp = np.zeros([
            z.shape[0], self.x_train.shape[1], self.x_train.shape[2],
            self.x_train.shape[3]
        ])
        return np.squeeze(self.generate_from_z(np.float32(inp)))


if __name__ == "__main__":
    data_dir = parent + "/data/"
    plot_dir = current + "/plots/"
    sound_dir = current + "/sound/"
    experiment_name = "vae"

    data = pickle_loader(
        data_dir + "sines.pkl"
    )  # data dictionary contains a "data" entry and a "sample rate" entry
    data = np.ndarray.astype(data, np.float32)

    data2 = data[:, 1:]
    data1 = data[:, :-1]

    dif = np.hstack([
        1 + np.absolute((data1 - data2) / 2),
        np.ones([data.shape[0], 1]).astype(np.float32)
    ])

    # map inputs from 0 to 1
    # patch and shape for the CNN
    mean = np.mean(data)
    std = np.std(data)
Example #5
0
	sample_rate = 880
	iterations = 100
	net_args =  {
	"dim_z" : 5,
	"learning_rate" : 0.0008,
	"batch_size" : 10,
	"pooling_d":3,
	"pooling_s":2,
	"dim_y" : 100,
	"filter_l":[10,10,10],
	"filter_no":[5,5,5]
	}


	##### DATA #######
	data= pickle_loader(data_dir+data_name) # data dictionary contains a "data" entry and a "sample rate" entry
	data = np.ndarray.astype(data, np.float32)
	data2 = data[:,1:]
	data1 = data[:,:-1]
	dif = np.hstack([1+np.absolute((data1-data2)/2),np.ones([data.shape[0],1]).astype(np.float32)])
	data = data.reshape(data.shape[0],1,data.shape[1],1)
	# train and test
	x_train = data[:200,:,:,:];y_train = np.zeros([x_train.shape[0],1],dtype="float32")
	#y_train=None
	dif = dif[:,:]

	####### NETWORK #########
	# Discover the magic number
	net = convVAE(x_train,y_train = y_train,**net_args)
	get_magic = net.get_flattened(net.x_train[:2,:,:,:])
	# actual nework
from preprocess import pickle_loader,pickle_saver,map_to_range,map_to_range_symmetric
import matplotlib.pyplot as plt
import numpy
from sn_plot import plot_reconstructed





parser = argparse.ArgumentParser()
parser.add_argument("-d","--double", help="Train on hidden layer of previously trained AE - specify params", default = False)

args = parser.parse_args()
#TOFO: Make all input convensions N*D
#x_train = pickle_loader("sound/test_data.pkl")
data_dictionary = pickle_loader("sound/mistakidis.pkl") # data dictionary contains a "data" entry and a "sample rate" entry
x_train = data_dictionary["data"][:20,:1000]
#print all_data.shape
x_test = x_train # x_test is the same as x_train


#x_train = np.array([numpy.sin(t*100), numpy.sin(t*200), numpy.sin(t*40), numpy.sin(t*50)])*.5

n_steps = 10000

dimZ = 4
HU_decoder = 400
HU_encoder = HU_decoder
batch_size =1
L = 1
learning_rate = 0.003