Ejemplo n.º 1
0
class MusicMatrix:
    # 음악을 인자로 받는다. 매트릭스 틀을 잡는 코드
    def __init__(self, song=None):
        self._previous_note = None

        # 음계와 쉼표 매트릭스를 따로 만든다
        if song is not None:
            notes = np.array(song, dtype=str)[:, 0]
            durations = np.array(song, dtype=str)[:, 1]

            for i, d in enumerate(durations):
                durations[i] = self.float2str(durations[i])

            # 음악에 쓰인 음표와 쉼표를 추출해서 매트릭스에 입력.
            self._markov = MarkovBuilder(np.unique(notes).tolist())
            self._timings = MarkovBuilder(np.unique(durations).tolist())

            for note in song:
                self.add(note)
        else:
            self._markov = MarkovBuilder(["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"])
            self._timings = MarkovBuilder([1, 2, 4, 8, 16])

        # print(self._markov._value_lookup)
        # print(self._timings._value_lookup)

    #쉼표 분수로 만들기.
    def float2str(self, d):
        if float(d) >= 1:
            return '%d' % int(float(d))
        else:
            return '%.2f' % float(d)


    def add(self, to_note):
        """Add a path from a note to another note. Re-adding a path between notes will increase the associated weight."""

        to_note = list(to_note)
        to_note[1] = self.float2str(to_note[1])

        if(self._previous_note is None):
            self._previous_note = to_note
            return
        from_note = self._previous_note
        self._markov.add(from_note[0], to_note[0])
        self._timings.add(from_note[1], to_note[1])
        self._previous_note = to_note
        
    def next_note(self, from_note):
        from_note = list(from_note)
        from_note[1] = self.float2str(from_note[1])

        return [self._markov.next_value(from_note[0]), float(self._timings.next_value(from_note[1]))]
Ejemplo n.º 2
0
    def __init__(self, song=None):
        self._previous_note = None

        # 음계와 쉼표 매트릭스를 따로 만든다
        if song is not None:
            notes = np.array(song, dtype=str)[:, 0]
            durations = np.array(song, dtype=str)[:, 1]

            for i, d in enumerate(durations):
                durations[i] = self.float2str(durations[i])

            # 음악에 쓰인 음표와 쉼표를 추출해서 매트릭스에 입력.
            self._markov = MarkovBuilder(np.unique(notes).tolist())
            self._timings = MarkovBuilder(np.unique(durations).tolist())

            for note in song:
                self.add(note)
        else:
            self._markov = MarkovBuilder(["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"])
            self._timings = MarkovBuilder([1, 2, 4, 8, 16])
Ejemplo n.º 3
0
class MusicMatrix:
    def __init__(self):
        self._previous_note = None
        self._markov = MarkovBuilder(["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"])
        self._timings = MarkovBuilder([1, 2, 4, 8, 16])

    def add(self, to_note):
        """Add a path from a note to another note. Re-adding a path between notes will increase the associated weight."""
        if(self._previous_note is None):
            self._previous_note = to_note
            return
        from_note = self._previous_note
        self._markov.add(from_note[0], to_note[0])
        self._timings.add(from_note[1], to_note[1])
        self._previous_note = to_note
        
    def next_note(self, from_note):
        return [self._markov.next_value(from_note[0]), self._timings.next_value(from_note[1])]
Ejemplo n.º 4
0
 def __init__(self):
     self._previous_note = None
     self._markov = MarkovBuilder(["a", "a#", "b", "c", "c#",
                                   "d", "d#", "e", "f", "f#", "g", "g#"])
     self._timings = MarkovBuilder([1, 2, 4, 8, 16])
Ejemplo n.º 5
0
 def __init__(self):
     self._previous_note = None
     self._markov = MarkovBuilder(["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"])
     self._timings = MarkovBuilder([1, 2, 4, 8, 16])
Ejemplo n.º 6
0
targetDir = './starwars/'
songs = [
    targetDir + f for f in listdir(targetDir)
    if isfile(join(targetDir, f)) and f.endswith('.mxl')
]
print(len(songs))
maxSongs = 15
order = 10
shuffle(songs)

# songs = ['spider.mxl', 'mega.mxl']

songPNGs = []
for song in songs[:maxSongs]:
    try:
        songPNGs.append(MIDI2PIXConverter(song, debug=True).buildImage())
    except:
        pass

markovName = 'storewors'
markovOutFile = markovName + str(order) + 'Out.png'
outMidiFile = markovName + str(order) + 'Out.mid'
# inputFile = 'spider.mxl'
# outFile = os.path.splitext(inputFile)[0] + '.png'
# markovOutFile = 'markov' + outFile
# outMidiFile = 'markov' + os.path.splitext(inputFile)[0] + '.mid'

MarkovBuilder(songPNGs, order=order).generateRandomSong(markovOutFile,
                                                        songLength=300)
PIX2MIDIConverter(markovOutFile).buildMidi(outMidiFile)