#pip install music21
#pip install RISE

from qiskit import QuantumCircuit, execute
from qiskit import Aer
from qiskit.visualization import plot_histogram
from qiskit import IBMQ
import qiskit.tools.jupyter
%qiskit_job_watcher
from IPython.display import Audio
import wave
import numpy as np

#start for the project



# import
from music21 import *

#minimum user input will just be for them to fill out the create quantum circuit function inthe backend
n = 1  #insert number of qubits
def CreateQuantumCircuit(n):
    cct = QuantumCircuit(n,1)
    cct.h(0)
    return cct

cct = CreateQuantumCircuit(n)
cct.draw(output="mpl")

def Measurement(cct):
    backend = Aer.get_backend('statevector_simulator')
    sv = execute(cct, backend).result().get_statevector()
    return sv

sv = Measurement(cct)
print(sv)
#from qiskit.visualization import plot_state_city
#plot_state_city(sv)





def MusicalTransformation():
    note = 'a';
    return note

tune_array = ['c','d'] #collect notes
#for i in range(n+1):
#  note = MusicalTransformation()
#  tune_array.append(note)
print(tune_array) 

def PlayPianoTune(character, song):
    if character == 'c':
        sound_file = "./Audio/Piano/1-st.wav"
        songs.append(sound_file)
    if character == 'd':
        sound_file = "./Audio/Piano/2-st.wav"
        songs.append(sound_file)
    if character == 'e':
        sound_file = "./Audio/Piano/3-st.wav"
        songs.append(sound_file)
    if character == 'f':
        sound_file = "./Audio/Piano/4-st.wav"
        songs.append(sound_file)
    if character == 'g':
        sound_file = "./Audio/Piano/5-st.wav"
        songs.append(sound_file)

def PlayGuitarTune(character, song):
    if character == 'c':
        sound_file = "./Audio/Guitar/1-ag.wav"
        songs.append(sound_file)
    if character == 'd':
        sound_file = "./Audio/Guitar/2-ag.wav"
        songs.append(sound_file)
    if character == 'e':
        sound_file = "./Audio/Guitar/3-ag.wav"
        songs.append(sound_file)
    if character == 'f':
        sound_file = "./Audio/Guitar/4-ag.wav"
        songs.append(sound_file)
    if character == 'g':
        sound_file = "./Audio/Guitar/5-ag.wav"
        songs.append(sound_file)

def PlayBassTune(character, song):
    if character == 'c':
        sound_file = "./Audio/Bass/1.wav"
        songs.append(sound_file)
    if character == 'd':
        sound_file = "./Audio/Bass/2.wav"
        songs.append(sound_file)
    if character == 'e':
        sound_file = "./Audio/Bass/3.wav"
        songs.append(sound_file)
    if character == 'f':
        sound_file = "./Audio/Bass/4.wav"
        songs.append(sound_file)
    if character == 'g':
        sound_file = "./Audio/Bass/5.wav"
        songs.append(sound_file)

def PlayTrumpetTune(character, song):
    if character == 'c':
        sound_file = "./Audio/Trumpet/1.wav"
        songs.append(sound_file)
    if character == 'd':
        sound_file = "./Audio/Trumpet/2.wav"
        songs.append(sound_file)
    if character == 'e':
        sound_file = "./Audio/Trumpet/3.wav"
        songs.append(sound_file)
    if character == 'f':
        sound_file = "./Audio/Trumpet/4.wav"
        songs.append(sound_file)
    if character == 'g':
        sound_file = "./Audio/Trumpet/5.wav"
        songs.append(sound_file)

songs = []
for i in range(n+1):
    character = tune_array[i-1]
    PlayPianoTune(character, songs)

print(songs)
outfile = "pianosounds.wav"
data= []
for song in songs:
    w = wave.open(song, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()

songs = []
for i in range(n+1):
    character = tune_array[i-1]
    PlayGuitarTune(character, songs)

print(songs)
outfile = "guitarsounds.wav"
data= []
for song in songs:
    w = wave.open(song, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()

songs = []
for i in range(n+1):
    character = tune_array[i-1]
    PlayTrumpetTune(character, songs)

print(songs)
outfile = "trumpetsounds.wav"
data= []
for song in songs:
    w = wave.open(song, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()

songs = []
for i in range(n+1):
    character = tune_array[i-1]
    PlayBassTune(character, songs)

print(songs)
outfile = "basssounds.wav"
data= []
for song in songs:
    w = wave.open(song, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()

sound_file = "./pianosounds.wav"
Audio(sound_file, autoplay=True)

sound_file = "./guitarsounds.wav"
Audio(sound_file, autoplay=True)

sound_file = "./basssounds.wav"
Audio(sound_file, autoplay=True)

sound_file = "./trumpetsounds.wav"
Audio(sound_file, autoplay=True)

up1 = note.Unpitched()
up1.storedInstrument = instrument.BassDrum()
upUnknownInstrument = note.Unpitched()

up2 = note.Unpitched()
up2.storedInstrument = instrument.Cowbell()
s = stream.Stream()
s.append(up1)
s.append(upUnknownInstrument)
s.append(up2)
s2 = instrument.unbundleInstruments(s)
s3 = instrument.bundleInstruments(s2)
for test in s3:
    print(test.storedInstrument)

instrument.ensembleNameBySize(4)

instrument.ensembleNameBySize(1)

instrument.ensembleNameBySize(83)

t1 = instrument.fromString('Clarinet 2 in A')
t1

t1.transposition

t2 = instrument.fromString('Clarinetto 3')
t2

t3 = instrument.fromString('flauto 2')
t3

t4 = instrument.fromString('I <3 music saxofono tenor go beavers')
t4

t5 = instrument.fromString('Bb Clarinet')
t5

t5.transposition

t6 = instrument.fromString('Clarinet in B-flat')
t5.__class__ == t6.__class__

t5.transposition == t6.transposition

t7 = instrument.fromString('B-flat Clarinet.')
t5.__class__ == t7.__class__ and t5.transposition == t7.transposition

t8 = instrument.fromString('Eb Clarinet')
t5.__class__ == t8.__class__

t8.transposition

t9 = instrument.fromString('Klarinette in B.')
t9

t9.transposition

instrument.instrumentFromMidiProgram(0)

instrument.instrumentFromMidiProgram(21)

p1 = converter.parse("tinynotation: 4/4 c4  d  e  f  g  a  b  c'  c1")
p2 = converter.parse("tinynotation: 4/4 C#4 D# E# F# G# A# B# c#  C#1")

p1.getElementsByClass('Measure')[0].insert(0.0, instrument.Piccolo())
p1.getElementsByClass('Measure')[0].insert(2.0, instrument.AltoSaxophone())
p1.getElementsByClass('Measure')[1].insert(3.0, instrument.Piccolo())

p2.getElementsByClass('Measure')[0].insert(0.0, instrument.Trombone())
p2.getElementsByClass('Measure')[0].insert(3.0, instrument.Piccolo())  # not likely...
p2.getElementsByClass('Measure')[1].insert(1.0, instrument.Trombone())

s = stream.Score()
s.insert(0, p1)
s.insert(0, p2)
s.show('musicxml') #this show on musicscore

s2 = instrument.partitionByInstrument(s)
len(s2.parts)

for p in s2.parts:
    unused = p.makeRests(fillGaps=True, inPlace=True)

for p in s2.parts:
    p.makeMeasures(inPlace=True)
    p.makeTies(inPlace=True)

s2.show('musicxml')

up1 = note.Unpitched()
up1.storedInstrument = instrument.BassDrum()
up2 = note.Unpitched()
up2.storedInstrument = instrument.Cowbell()
s = stream.Stream()
s.append(up1)
s.append(up2)
s2 = instrument.unbundleInstruments(s)
s2.show('musicxml')





us = environment.UserSettings()
#C:\Program Files\MuseScore 3\bin\MuseScore3.exe
us['musicxmlPath'] = "C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe"
us['musescoreDirectPNGPath'] = "C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe"
print(us)

instrument1 = 60
instrument2 = 65
instrument3 = 70
instrument4 = 75

instrument1 = converter.parse("tinynotation: 4/4 c4  d  e  f  g  a  b  c'  c1")
instrument2 = converter.parse("tinynotation: 4/4 C#4 D# E# F# G# A# B# c#  C#1")
instrument3 = converter.parse("tinynotation: 4/4 e   f  g  a  b  c  d  e   f")
#instrument4 = converter.parse("tinynotation: 4/4 g   a  b  c  d  e  f  g   a")





note1 = note.Note("C4")
note2 = note.Note("F#4")
note3 = note.Note("F#4")
note4 = note.Note("F#4")
note5 = note.Note("F#4")
note6 = note.Note("F#4")
note7 = note.Note("F#4")
note8 = note.Note("F#4")
note9 = note.Note("F#4")
instrument4 = [note1, note2, note3, note4, note5, note6, note7, note8, note9]

print(instrument4)

stream2 = stream.Stream()
stream2.append(instrument4)

stream2.getElementsByClass('Measure')[0].insert(0.0, instrument.Tuba())

#This creates a stream of a single part
#Add an instrument to the part

instrument1.getElementsByClass('Measure')[0].insert(0.0, instrument.Trumpet())
instrument2.getElementsByClass('Measure')[0].insert(0.0, instrument.EnglishHorn())
instrument3.getElementsByClass('Measure')[0].insert(0.0, instrument.Trombone())
instrument4.getElementsByClass('Measure')[0].insert(0.0, instrument.Tuba())

s = stream.Score()
s.insert(0, instrument1)
s.insert(0, instrument2)
s.insert(0, instrument3)
s.insert(0, instrument4)

s.show()

s.show('midi')

s.show('musicxml') #this show on musicscore

stream1 = stream.Stream()
stream1.addGroupForElements('flute')
stream1.append(midiChordType1)
stream1.append(midiChordType2)
stream1.append(midiChordType3)
stream1.append(midiChordType4)
stream1.show('midi')
stream1.show('musicxml') #this show on musicscore
#c = converter.parse('musicxml')
stream1.show()

midiChordType1 = chord.Chord([instrument1, instrument2, instrument3, instrument4])

midiChordType2 = chord.Chord([55, 60, 65, 70])
midiChordType2.getElementsByClass('Measure')[0].insert(2.0, instrument.Bass())
                                                       
midiChordType3 = chord.Chord([50, 55, 60, 70])
midiChordType3.getElementsByClass('Measure')[1].insert(3.0, instrument.Guitar())
                                                       
midiChordType4 = chord.Chord([65, 70, 75, 80])
midiChordType4.getElementsByClass('Measure')[1].insert(3.0, instrument.Trombone())

note1 = note.Note("C4")
note2 = note.Note("F#4")
note3 = note.Note("B-2")
stream1 = stream.Stream()
stream1.append(note1)
stream1.append(note2)
stream1.append(note3)

stream1.show('midi')

note1 = midiChordType

midiChordType1 = chord.Chord([60, 65, 70, 75])
midiChordType2 = chord.Chord([55, 60, 65, 70])
midiChordType3 = chord.Chord([50, 55, 60, 70])
midiChordType4 = chord.Chord([65, 70, 75, 80])
stream1 = stream.Stream()
stream1.append(midiChordType1)
stream1.append(midiChordType2)
stream1.append(midiChordType3)
stream1.append(midiChordType4)
stream1.show('midi')

p1 = converter.parse("tinynotation: 4/4 c4  d  e  f  g  a  b  c'  c1")
p2 = converter.parse("tinynotation: 4/4 C#4 D# E# F# G# A# B# c#  C#1")

p1.show('midi')

p2.show('midi')

p1.getElementsByClass('Measure')[0].insert(0.0, instrument.Piccolo())
p1.getElementsByClass('Measure')[0].insert(2.0, instrument.AltoSaxophone())
p1.getElementsByClass('Measure')[1].insert(3.0, instrument.Piccolo())

p2.getElementsByClass('Measure')[0].insert(0.0, instrument.Trombone())
p2.getElementsByClass('Measure')[0].insert(3.0, instrument.Piccolo())  # not likely...
p2.getElementsByClass('Measure')[1].insert(1.0, instrument.Trombone())



s.show()

s = corpus.parse('bach/bwv65.2.xml')

