Пример #1
0
def generateWithCAndP(model,
                      seed,
                      mid_name,
                      temperature=1.0,
                      length=512,
                      channelInput=14,
                      programInput=117):
    '''Generate sequence using model, seed, and temperature.'''

    generated = []
    phrase = seed

    if not hasattr(temperature, '__len__'):
        temperature = [temperature for _ in range(length)]

    for temp in temperature:
        x = np.zeros((1, PHRASE_LEN, SYMBOL_DIM))
        for t, config_id in enumerate(phrase):
            x[0, t, config_id] = 1
        preds = model.predict(x, verbose=0)[0]
        next_id = sample(preds, temp)

        generated += [next_id]
        phrase = phrase[1:] + [next_id]

    mid = array_to_midiWithProgramAndChannel(unfold(decode(generated),
                                                    OUT_PITCHES),
                                             mid_name,
                                             channelInput=channelInput,
                                             programInput=programInput)
    print("the mid is + " + str(type(mid)))
    for element in mid:
        print("the intitial mid is " + str(element))
        #element = updateValue(element)
        #element.time = int(element.time)
        #print("theChecko " + str(type(element)))
    mid = updateValue(mid)
    # for element in mid.tracks:
    #     for msg in element:
    #         print("elmentsdfdf s " + str(msg))
    #
    # print("the mid_name is + " + str(mid_name))
    # midiFileToReturn = MidiFile()
    # # track = []
    # # midiFileToReturn.tracks.append(track)
    # tmpTime = 0
    # # for element in midiFileInput:
    # #    print("notrack: "+ str(element))
    # for i, element in enumerate(mid.tracks):
    #     tmpTrack = MidiTrack()
    #     midiFileToReturn.tracks.append(tmpTrack)
    #     print("inFOrefe is " + str(element))
    #     # if(element.type == "note_on" or element.type == "note_off"):
    #     # print("timoIs " + str(element.time))
    #     ##element.time = int(element.time*480)
    #     # element.time = int(element.time*480)
    #     # tmpTime+=1
    #     # track.append(element)
    #     for msg in element:
    #         msg.time = int(msg.time)
    #         print("track" + str(msg.time))
    #         tmpTrack.append(msg)

    mid.save(os.path.join(mid_name))
    return mid
Пример #2
0
def generateWithCAndPHipHop(model, seed, mid_name, temperature=1.0, length=512,channelInput = 14, programInput = 117,getArray=False,percussion=0,basse=0,guitare=0,violon=0,saxophone=0,piano=0,musicType=0):
    '''Generate sequence using model, seed, and temperature.'''

    generated = []
    phrase = seed
    if not hasattr(temperature, '__len__'):
        temperature = [temperature for _ in range(length)]

    for temp in temperature:
        x = np.zeros((1, PHRASE_LEN, SYMBOL_DIM))
        for t, config_id in enumerate(phrase):
            x[0, t, config_id] = 1
        preds = model.predict(x, verbose=0)[0]
        next_id = sample(preds, temp)

        generated += [next_id]
        phrase = phrase[1:] + [next_id]

    if(getArray == True):
        return generated
    if (musicType == 0):
            generatedForDrums=[]
            for x in range(4,len(generated),4):
                tmpGen = generated[x-4:x]
                for index,tmpX in enumerate(tmpGen):
                    if(tmpX > 0):
                        break
                    else:
                        if(index == len(tmpGen)-1):
                            tmpGen[index] = 1002
                generatedForDrums+=tmpGen
            numberOfActivatedNotes = findNumberOfActivatedNotes(generated)
            generatedForDrums2 = harMonize(generated,3)
            mid = array_to_midiWithProgramAndChannel(unfold(decode(generated), OUT_PITCHES), mid_name,channelInput=channelInput,programInput=programInput)

            mid = updateValue(mid)
            mid.save(os.path.join("music/pianoHiphop.mid"))
            if (percussion == 1):
                generatedForDrums3 = generateFromLoaded2HipHop("hdfFiveFiles/drumshiphop.hdf5",
                                                     "music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineDrums.mid", "Drums", 1,
                                                     True)
                generatedForDrums4 = harMonize(generatedForDrums3,numberOfActivatedNotes)
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineDrums.mid")
                mid2 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForDrums4), OUT_PITCHES), "drumsForMid_name.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
                mid2 = updateValue(mid2)
                mid2.save(os.path.join("music/drumsHiphop.mid"))
            else :
                generatedForDrums4 = [0]
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineDrums.mid")
                mid2 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForDrums4), OUT_PITCHES),
                                                          "music/drumsForMid_name.mid",
                                                          channelInput=tmpChan, programInput=tmpProgram)
                mid2 = updateValue(mid2)
                mid2.save(os.path.join("music/drumsHiphop.mid"))

            #GUITAR
            if (guitare == 1):
                generatedForGuitar = generateFromLoaded2HipHop("hdfFiveFiles/guitar2hiphop.hdf5",
                                                     "music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineGuitar.mid", "Guitar", 1,
                                                     True)
                generatedForGuitar2 = harMonize(generatedForGuitar, numberOfActivatedNotes)
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineGuitar.mid")
                mid3 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForGuitar2), OUT_PITCHES), "guitarForMid_name.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
                mid3 = updateValue(mid3)
                mid3.save(os.path.join("music/guitareHiphop.mid"))
            else:
                generatedForGuitar2 = [0]
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineGuitar.mid")
                mid3 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForGuitar2), OUT_PITCHES),
                                                          "music/guitarForMid_name.mid",
                                                          channelInput=tmpChan, programInput=tmpProgram)
                mid3 = updateValue(mid3)
                mid3.save(os.path.join("music/guitareHiphop.mid"))

            # Bass
            if (basse == 1):
                generatedForBass = generateFromLoaded2HipHop("hdfFiveFiles/basshiphop.hdf5",
                                                     "music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineBass.mid", "Bass", 1,
                                                     True)
                generatedForBass2 = harMonize(generatedForBass, numberOfActivatedNotes)
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineBass.mid")
                mid4 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForBass2), OUT_PITCHES), "bassForMid_name.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
                mid4 = updateValue(mid4)
                mid4.save(os.path.join("music/bassHiphop.mid"))

            else:
                generatedForBass2 = [0]
                tmpChan, tmpProgram = getChannelAndProgam("music/Marvin_Gaye_-_I_Heard_It_Through_the_GrapevineBass.mid")
                mid4 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForBass2), OUT_PITCHES), "bassForMid_name.mid",
                                                          channelInput=tmpChan, programInput=tmpProgram)
                mid4 = updateValue(mid4)
                mid4.save(os.path.join("music/bassHiphop.mid"))

            merger(["pianoHiphop","drumsHiphop","bassHiphop","guitareHiphop",],"HipHop.mid")
            return mid

    elif (musicType == 1):
        numberOfActivatedNotes = findNumberOfActivatedNotes(generated)
        mid = array_to_midiWithProgramAndChannel(unfold(decode(generated), OUT_PITCHES), mid_name,
                                                 channelInput=channelInput, programInput=programInput)
        mid = updateValue(mid)
        mid.save(os.path.join("music/pianoPop.mid"))
        if (guitare == 1):
            generatedForGuitare3 = generateFromLoaded2HipHop("hdfFiveFiles/london_bridge.hdf5",
                                                           "music/london_bridge.mid",
                                                           "Guitare", 1,
                                                           True)
            generatedForGuitare4 = harMonize(generatedForGuitare3, numberOfActivatedNotes)
            tmpChan, tmpProgram = getChannelAndProgam("music/london_bridge.mid")
            mid2 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForGuitare4), OUT_PITCHES),
                                                      "music/london_bridge.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
            mid2 = updateValue(mid2)
            mid2.save(os.path.join("music/guitarePop.mid"))
        else:
            generatedForGuitare4 = [0]
            tmpChan, tmpProgram = getChannelAndProgam("music/london_bridge.mid")
            mid2 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForGuitare4), OUT_PITCHES),
                                                      "music/london_bridge.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
            mid2 = updateValue(mid2)
            mid2.save(os.path.join("music/guitarePop.mid"))


        if (violon == 1):
            generatedForViolin = generateFromLoaded2HipHop("hdfFiveFiles/10_little_indiansvln.hdf5",
                                                           "music/10_little_indiansvln.mid",
                                                           "Violin", 1,
                                                           True)
            generatedForViolin2 = harMonize(generatedForViolin, numberOfActivatedNotes)
            tmpChan, tmpProgram = getChannelAndProgam("music/10_little_indiansvln.mid")
            mid3 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForViolin2), OUT_PITCHES),
                                                      "music/10_little_indiansvln.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
            mid3 = updateValue(mid3)
            mid3.save(os.path.join("music/violinPop.mid"))
        else:
            generatedForViolin2 = [0]
            tmpChan, tmpProgram = getChannelAndProgam("music/10_little_indiansvln.mid")
            mid3 = array_to_midiWithProgramAndChannel(unfold(decode(generatedForViolin2), OUT_PITCHES),
                                                      "music/10_little_indiansvln.mid",
                                                      channelInput=tmpChan, programInput=tmpProgram)
            mid3 = updateValue(mid3)
            mid3.save(os.path.join("music/violinPop.mid"))
        merger(["pianoPop","guitarePop","violinPop",],"Pop.mid")
        return  mid
    elif (musicType == 2):
        mid = array_to_midiWithProgramAndChannel(unfold(decode(generated), OUT_PITCHES), mid_name,
                                                     channelInput=channelInput, programInput=programInput)
        mid = updateValue(mid)
        mid.save(os.path.join("music/saxoJazz.mid"))
        merger(["saxoJazz", ], "Jazz.mid")
    elif(musicType == 3):
        mid = array_to_midiWithProgramAndChannel(unfold(decode(generated), OUT_PITCHES), mid_name,
                                                 channelInput=channelInput, programInput=programInput)
        mid = updateValue(mid)
        mid.save(os.path.join("music/classical.mid"))
        return mid