CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 1/Jazz improvisation with LSTM/__pycache__/preprocess.cpython-36.pyc
Views: 13383
3

��nZf�@sxdZddlmZddlTddlmZmZddlmZm	Z	ddl
Tddl
mZddlTdd�Z
d	d
�Zdd�Zd
d�ZdS)z�
Author:     Ji-Sung Kim
Project:    deepjazz
Purpose:    Parse, cleanup and process data.

Code adapted from Evan Chow's jazzml, https://github.com/evancchow/jazzml with
express permission.
�)�print_function)�*)�defaultdict�OrderedDict)�groupby�zip_longest)�parse_melodycsjtj|�}|d}|jtj�\}}x|D]}|j|j|�q(W|}x|D]}|jdkrHd|_qHW|jdtj	��|jdt
jdd��ddddg�tj�}|j�fd	d
�t
|�D��tj�}	x"tt|��D]}|	j||�q�W|	j|�tj�}
x�|	D]z}tj�}|j|jtj��|j|jtj��|j|jt
j��|j|jtj��|j|jddd
d��|j}
|
j|
�q�W|
d}t�}dd
�|D�}d}x6t|dd��D]$\}}dd
�|D�||<|d7}�q�W|
d}|jtj�|jtj�dd
�|D�}t�}d}x6t|dd��D]$\}}dd
�|D�||<|d7}�qW|t|�d=t|�t|�k�sbt�||fS)N�gg�?r�)�sharps��csg|]\}}|�kr|j�qS�)�flat)�.0�i�j)�partIndicesr�C/home/jovyan/work/Week 1/Jazz improvisation with LSTM/preprocess.py�
<listcomp>1sz __parse_midi.<locals>.<listcomp>i�i$T)�includeEndBoundarycSsg|]}t|jd�|f�qS)�)�int�offset)r�nrrrrPscSs|dS)Nrr)�xrrr�<lambda>Rsz__parse_midi.<locals>.<lambda>cSsg|]}|d�qS)r
r)rrrrrrSscSsg|]}t|jd�|f�qS)r)rr)rrrrrr[scSs|dS)Nrr)rrrrrbscSsg|]}|d�qS)r
r)rrrrrrcs�����)�	converter�parse�getElementsByClass�stream�Voice�insertr�
quarterLength�
instrument�ElectricGuitar�key�KeySignature�append�	enumerate�range�len�Part�
Instrument�tempo�
MetronomeMark�meter�
TimeSignature�getElementsByOffsetrrr�
removeByClass�note�Rest�Note�AssertionError)�data_fnZ	midi_dataZ
melody_streamZmelody1Zmelody2rZmelody_voicerZcomp_streamZfull_streamZsolo_stream�partZ	curr_part�cp�measuresZoffsetTuples�
measureNumZkey_x�groupZchordStreamZoffsetTuples_chords�chordsr)rr�__parse_midis`







	r@c	Cs�g}xztdt|��D]h}tj�}x||D]}|j|j|�q*Wtj�}x||D]}|j|j|�qRWt||�}|j|�qW|S)Nr
)r+r,r!r"r#rrr))	r<r?�abstract_grammars�ix�mr�cr�parsedrrr�__get_abstract_grammarsss
rFcCst|�\}}t||�}||fS)N)r@rF)r9r<r?rArrr�get_musical_data�s
rGcCsNdd�|D�}t|�}tdd�t|�D��}tdd�t|�D��}||||fS)NcSs g|]}|jd�D]}|�qqS)� )�split)r�sublistrrrrr�sz#get_corpus_data.<locals>.<listcomp>css|]\}}||fVqdS)Nr)rr�vrrr�	<genexpr>�sz"get_corpus_data.<locals>.<genexpr>css|]\}}||fVqdS)Nr)rrrKrrrrL�s)�set�dictr*)rA�corpus�valuesZval_indicesZindices_valrrr�get_corpus_data�s
rQN)�__doc__�
__future__r�music21�collectionsrr�	itertoolsrr�grammarr�music_utilsr@rFrGrQrrrr�<module>s[