示例#1
0
def play_basic_chord(chord):
    c = NoteContainer(chord)
    l = Note(c[0].name)
    l.octave_down()
    print ch.determine(chord)[0]

    # Play chord and lowered first note
    fluidsynth.play_NoteContainer(c)
    fluidsynth.play_Note(l)
    time.sleep(1.0)

    return c
示例#2
0
def play_basic_chord(chord):
   c = NoteContainer(chord)
   l = Note(c[0].name)
   l.octave_down()
   print ch.determine(chord)[0]
	
   # Play chord and lowered first note
   fluidsynth.play_NoteContainer(c)
   fluidsynth.play_Note(l)
   time.sleep(1.0)
	
   return c
示例#3
0
def playProgression():
    progression = ["I", "vi", "ii", "iii7", "I7", "viidom7", "iii7", "V7"]
    key = "C"

    chords = progressions.to_chords(progression, key)

    if not fluidsynth.init(SF2):
        print "Couldn't load soundfont", SF2
        sys.exit(1)

    while 1:
        i = 0
        for chord in chords:
            c = NoteContainer(chords[i])
            l = Note(c[0].name)
            p = c[1]
            l.octave_down()
            print ch.determine(chords[i])[0]

            # Play chord and lowered first note
            fluidsynth.play_NoteContainer(c)
            fluidsynth.play_Note(l)
            time.sleep(1.0)

            # Play highest note in chord
            fluidsynth.play_Note(c[-1])

            # 50% chance on a bass note
            if random() > 0.5:
                p = Note(c[1].name)
                p.octave_down()
                fluidsynth.play_Note(p)
            time.sleep(0.50)

            # 50% chance on a ninth
            if random() > 0.5:
                l = Note(intervals.second(c[0].name, key))
                l.octave_up()
                fluidsynth.play_Note(l)
            time.sleep(0.25)

            # 50% chance on the second highest note
            if random() > 0.5:
                fluidsynth.play_Note(c[-2])
            time.sleep(0.25)

            fluidsynth.stop_NoteContainer(c)
            fluidsynth.stop_Note(l)
            fluidsynth.stop_Note(p)
            i += 1
        print "-" * 20
示例#4
0
def play_chord(chord):
    c = play_basic_chord(chord)

    # Play highest note in chord
    fluidsynth.play_Note(c[-1])

    # 50% chance on a bass note
    if random() > 0.5:
        p = Note(c[1].name)
        p.octave_down()
        fluidsynth.play_Note(p)
    time.sleep(0.50)

    # 50% chance on a ninth
    if random() > 0.5:
        p = Note(intervals.second(c[0].name, key))
        p.octave_up()
        fluidsynth.play_Note(p)
    time.sleep(0.25)

    # 50% chance on the second highest note
    if random() > 0.5:
        fluidsynth.play_Note(c[-2])
    time.sleep(0.25)
示例#5
0
def play_chord(chord):
   c = play_basic_chord(chord)

   # Play highest note in chord
   fluidsynth.play_Note(c[-1])

   # 50% chance on a bass note
   if random() > 0.5:
      p = Note(c[1].name)
      p.octave_down()
      fluidsynth.play_Note(p)
   time.sleep(0.50)
		
   # 50% chance on a ninth
   if random() > 0.5:
      p = Note(intervals.second(c[0].name, key))
      p.octave_up()
      fluidsynth.play_Note(p)
   time.sleep(0.25)

   # 50% chance on the second highest note
   if random() > 0.5:
      fluidsynth.play_Note(c[-2])
   time.sleep(0.25)
示例#6
0
#音对象
from mingus.containers import Note
#C4音对象
n = Note('C')
#变为C5
n.set_note('C', 5)
#音对象属性
n.name  #十二音名
n.octave  #第几八度
n.dynamics  #其它属性
#音对象方法
int(n)  #音对象的数值
c = Note()
c.from_int(12)  #使用数值创建音对象
c.octave_up()  #升八度
c.octave_down()  #降八度
c.change_octave(2)  #升n八度
c.transpose('3', up=True)  #向上升三度
c.augment()  #升半音
c.diminish()  #降半音
c.remove_redundant_accidentals()  #清理多余升降号(只能成对清理,烂)

#谱容器
from mingus.containers import NoteContainer
#创建谱容器对象(继承列表,完全可以按列表操作,不用看下面的)
n = NoteContainer(['A-3', 'C-5', 'B-4'])
n.add_note('F-1')  # 0位加音
n.remove_note('B', 4)  #删音
n.empty()  #清空

#乐器音色
示例#7
0
solo_channel = 13
random_solo_channel = False
if not fluidsynth.init(SF2):
    print "Couldn't load soundfont", SF2
    sys.exit(1)
chords = progressions.to_chords(progression, key)
loop = 1
while loop < song_end:
    i = 0
    if random_solo_channel:
        solo_channel = choice(range(5, 8) + [11])
    for chord in chords:
        c = NoteContainer(chords[i])
        l = Note(c[0].name)
        n = Note('C')
        l.octave_down()
        l.octave_down()
        print ch.determine(chords[i])[0]

        if not swing and play_chords and loop > chord_start and loop\
             < chord_end:
            fluidsynth.play_NoteContainer(c, chord_channel, randrange(50, 75))
        if play_chords and loop > chord_start and loop < chord_end:
            if orchestrate_second:
                if loop % 2 == 0:
                    fluidsynth.play_NoteContainer(c, chord_channel2,
                            randrange(50, 75))
            else:
                fluidsynth.play_NoteContainer(c, chord_channel2, randrange(50,
                        75))
示例#8
0
def generate_accompaniment(net_output_chords):
    generated_chords = []
    chords = []
    for chord in net_output_chords:
        root, key = (chord.split(":"))
        print(root, key)
        if key == 'maj':
            chords.append(ch.major_triad(root))
        if key == 'min':
            chords.append(ch.minor_triad(root))      

    print(chords)  

    key = chords[0][0]
    print('key', key)
    if not fluidsynth.init(SF2):
        print("Couldn't load soundfont", SF2)
        sys.exit(1)

    print(dir(fluidsynth.midi))
    # fluidsynth.midi.start_audio_output()
    fluidsynth.midi.start_recording()
    phrase = 0
    while phrase == 0:
        i = 0
        for chord in chords:
            print("chord", chord)
            c = NoteContainer(chords[i])
            generated_chords.append([{'note':cc.name.replace("B#","B").replace("E#","E").replace("##","#"), 'octave':cc.octave} for cc in c])
            l = Note(c[0].name)
            p = c[1]
            l.octave_down()
            print(ch.determine(chords[i])[0])

            # Play chord and lowered first note
            # fluidsynth.midi.MidiFileOut.write_NoteContainer("test.mid", c)
            print("NEW CHORD = ", c)

            if PLAY_ENABLED:

                fluidsynth.play_NoteContainer(c)
                fluidsynth.play_Note(l)
                time.sleep(1.0)

                # Play highest note in chord
                fluidsynth.play_Note(c[-1])

                # 50% chance on a bass note

                if random() > 0.50:
                    p = Note(c[1].name)
                    p.octave_down()
                    fluidsynth.play_Note(p)
                time.sleep(0.50)

                # 50% chance on a ninth

                if random() > 0.50:
                    l = Note(intervals.second(c[0].name, key))
                    l.octave_up()
                    fluidsynth.play_Note(l)
                time.sleep(0.25)

                # 50% chance on the second highest note

                if random() > 0.50:
                    fluidsynth.play_Note(c[-2])
                time.sleep(0.25)
                fluidsynth.stop_NoteContainer(c)
                fluidsynth.stop_Note(l)
                fluidsynth.stop_Note(p)
            i += 1
        print("-" * 20)
        phrase = 1
        return generated_chords
    def make_frame(self, t):
        # Setup landscape
        im = plim.new('RGB', size)
        draw = ImageDraw.Draw(im)

        # If the next note time is up - add it to the spool
        if (self.frame < len(pick[0])) and (t > float(
                str(pick[1][self.frame])[:5])):
            # Reset vars.
            pos = []
            brk = False

            # If the note's magnitude is above the threshold, and standard deviation for salience, show the note
            if (int(pick[2][self.frame]) >= self.threshold) and (float(
                    pick[5][self.frame]) > std):
                # Convert note into string, position, sharpness & how it appears in video
                try:
                    # Gets the current note from pickle and format
                    note = str(pick[0][self.frame][0]).replace("'", "")
                    note = note[:len(note) - 1] + '-' + note[len(note) - 1:]
                    note_len = len(note)
                    note_str = note
                    note = Note(note)
                    loop_counter = 0

                    # Enables note switching of octaves
                    while True:
                        if (len(instrument.find_fingering(
                            [note])) == 0) or (instrument.find_fingering(
                                [note])[0][0][1] > int(instrument_info[4][0])):
                            # Finger position is unavailable
                            if len(instrument.find_fingering([note])) == 0:
                                if int(note_str[note_len - 1:]) > int(
                                        instrument_info[4][0]):
                                    note.octave_down()
                                else:
                                    note.octave_up()
                            # Finger position is greater than max
                            elif instrument.find_fingering(
                                [note])[0][0][1] > int(instrument_info[4][0]):
                                note.octave_down()
                        else:
                            break
                        if loop_counter == 6:
                            brk = True
                            break
                        loop_counter += 1

                    # Something something something
                    note = str(note).replace("'", "")
                    string, pos_note = instrument.find_fingering([note])[0][0]
                    position.append(string)

                    # Color of animation depending on pitch grade
                    if pos_note == (int(instrument_info[4][0]) + 1):
                        pos_note -= 1
                        color = higher
                    elif ('#' in note) and (pos_note != 0):
                        color = higher
                    elif ('#' in note) and (pos_note == 0):
                        pos_note += 1
                        color = lower
                    elif 'b' in note:
                        color = lower
                    else:
                        color = norm

                    # Get the position of note for animation
                    pos = instrument_info[0][string]

                # Error with converting note - omit it from spooler
                except Exception as e:
                    position.append('10')
                    brk = True
            else:
                position.append('10')
                brk = True

            # Found a note to use
            if brk == False:
                # Add note and timing to the spooler
                self.add_note(pos, pos_note, t, color)

                # Saves Sharp/Flat for later
                if '#' in note:
                    self.modifier += 1
                elif 'b' in note:
                    self.modifier -= 1

                # Add the generated sheet music
                try:
                    determine = self.getProximity()
                    note = note.replace('-', '').replace('#', '')
                    if determine == 0.25:
                        note_type = 'quarter'
                    elif determine == 0.5:
                        note_type = 'half'
                    elif determine == 0.9:
                        note_type = 'whole'
                except Exception as e:
                    note_type = 'quarter'
                    pass
                note = (note.replace('#', '')).replace('-', '')
                try:
                    staff_position = staff_notes_high.index(note)
                    y = 265
                    if int(note[1:]) >= 5:
                        if note_type != 'whole':
                            self.add_lower_line(y + (staff_position * 1.8), t)
                        if staff_position <= 8:
                            for x in range(8, staff_position - 1, -2):
                                self.add_underline(y + (x * 1.8), t)
                    else:
                        if note_type != 'whole':
                            self.add_upper_line(y + (staff_position * 1.8), t)
                except Exception as e:
                    staff_position = staff_notes_low.index(note)
                    y = 310
                    if int(note[1:]) == 3:
                        if note_type != 'whole':
                            self.add_lower_line(y + (staff_position * 1.8), t)
                    else:
                        if note_type != 'whole':
                            self.add_upper_line(y + (staff_position * 1.8), t)
                        if staff_position >= 11:
                            for x in range(11, staff_position + 1, 2):
                                self.add_underline(y + (x * 1.8), t)

                # Add note and timing to the spooler for sheet music
                self.add_circle(note_type, y + (staff_position * 1.8), t,
                                self.modifier)

                # Add sharp icon if present in note
                if self.modifier == 1:
                    self.add_sharp(y + (staff_position * 1.8), t)

            # If the spooler is empty - get it ready
            if len(self.spool) == 0:
                self.last_pop_frame = self.frame
                self.last_pop_frame_time = str(pick[1][self.frame])[:5]

            # Increment to the next note
            self.frame += 1

        # Check to see if a note needs to be popped from spool:
        if len(self.spool_note_timer) > 0:
            # If the note is done being played - pop it from the spooler
            if (t - float(self.spool_note_timer[0])) > float(
                    instrument_info[1][0]):
                self.spool.pop(0)
                self.spool_note_timer.pop(0)
                self.last_pop_frame += 1
                if (self.frame < len(pick[0])):
                    self.last_pop_frame_time = str(
                        pick[1][self.last_pop_frame])[:5]
        # Check to see if a symbol (circle with a line) for sheet music nees to be popped as well
        if len(self.spool_circle_timer) > 0:
            # If the circle is done being shown - pop it from the spooler
            if (t - float(self.spool_circle_timer[0])) > float(
                    instrument_info[1][0]):
                self.spool_circle.pop(0)
                self.spool_circle_timer.pop(0)
        # Lines pop
        if len(self.spool_line_timer) > 0:
            # If the line is done being shown - pop it from the spooler
            while True:
                try:
                    if (t - float(self.spool_line_timer[0])) > float(
                            instrument_info[1][0]):
                        self.spool_line.pop(0)
                        self.spool_line_timer.pop(0)
                    else:
                        break
                except Exception as e:
                    break
        # Sharp symbol pop
        if len(self.spool_sharp_timer) > 0:
            while True:
                try:
                    # If the line is done being shown - pop it from the spooler
                    if (t - float(self.spool_sharp_timer[0])) > float(
                            instrument_info[1][0]):
                        for x in range(0, 4):
                            self.spool_sharp.pop(x)
                            self.spool_sharp_timer.pop(x)
                    else:
                        break
                except Exception as e:
                    break

        # Draw the notes from spool if not empty
        for x in range(0, len(self.spool)):
            exec(self.spool[x])

        # Draw the circles from spool if not empty
        for x in range(0, len(self.spool_circle)):
            exec(self.spool_circle[x])

        # Setup for drawing lines
        drawl = aggdraw.Draw(im)
        drawl.setantialias(False)
        p = aggdraw.Pen(color=black, width=2)
        p_thin = aggdraw.Pen(color=black, width=1)

        # Draw the lines from spool if not empty
        for x in range(0, len(self.spool_line)):
            exec(self.spool_line[x])

        # Draw the sharps from spool if not empty
        for x in range(0, len(self.spool_sharp)):
            exec(self.spool_sharp[x])

        # Whoosh!
        drawl.flush()

        # Returns the instance frame to be added to the movie
        return PIL_to_npimage(im)
示例#10
0
	def make_frame(self,t):
		# Setup landscape
		im = plim.new('RGB',size)
		draw = ImageDraw.Draw(im)

		# If the next note time is up - add it to the spool
		if (self.frame < len(pick[0])) and (t > float(str(pick[1][self.frame])[:5])):
			# Reset vars.
			pos = []
			brk = False

			# If the note's magnitude is above the threshold, show the note
			if int(pick[2][self.frame]) >= self.threshold:
				# Convert note into string, position, sharpness & how it appears in video
				try:
					# If the note is within frequency range
					#if int(instrument_info[3][1][0]) > float(librosa.note_to_hz(pick[0][self.frame][0])[0]) > int(instrument_info[3][0][0]):
					# Gets the current note from pickle and format
					note = str(pick[0][self.frame][0]).replace("'","")
					note = note[:len(note)-1] + '-' + note[len(note)-1:]
					note_len = len(note)
					note_str = note
					note = Note(note)
					loop_counter = 0

					while True:
						if (len(instrument.find_fingering([note])) == 0) or (instrument.find_fingering([note])[0][0][1] > int(instrument_info[4][0])):
							# Finger position is unavailable
							if len(instrument.find_fingering([note])) == 0:
								if int(note_str[note_len-1:]) > int(instrument_info[4][0]):
									note.octave_down()
								else:
									note.octave_up()
							# Finger position is greater than max
							elif instrument.find_fingering([note])[0][0][1] > int(instrument_info[4][0]):
								note.octave_down()
						else:
							break
						if loop_counter == 6:
							brk = True
							break
						loop_counter += 1
					if brk == False:
						note = str(note).replace("'","")
						string, pos_note = instrument.find_fingering([note])[0][0]
						position.append(string)

						# Color of animation depending on pitch grade
						if pos_note == (int(instrument_info[4][0])+1):
							pos_note -= 1
							color = higher
						elif ('#' in note) and (pos_note != 0):
							color = higher
						elif ('#' in note) and (pos_note == 0):
							pos_note += 1
							color = lower
						elif 'b' in note:
							color = lower
						else:
							color = norm

						# Get the position of note for animation
						pos = instrument_info[0][string]
					else:
						position.append('10')
					#else:
					#	position.append('10')
					#	brk = True
				# Error with converting note - omit it from spooler
				except Exception as e:
					position.append('10')
					brk = True
			else:
				position.append('10')
				brk = True

			# Found a note to use
			if brk == False:
				# Add note and timing to the spooler
				self.add(pos,pos_note,t,color)

				# If the spooler is empty - get it ready
				if len(self.spool) == 0:
					self.last_pop_frame = self.frame
					self.last_pop_frame_time = str(pick[1][self.frame])[:5]

			# Increment to the next note
			self.frame += 1

		# Check to see if a note needs to be popped from spool:
		if len(self.spool_timer) > 0:
			# If the note is done being played - pop it from the spooler
			if (t - float(self.spool_timer[0])) > float(instrument_info[1][0]):
				self.spool.pop(0)
				self.spool_timer.pop(0)
				self.last_pop_frame += 1
				self.last_pop_frame_time = str(pick[1][self.last_pop_frame])[:5]

		# Draw the lines from spool if not empty
		for x in range(0,len(self.spool)):
			exec(self.spool[x])
		
		# Returns the instance frame to be added to the movie
		return PIL_to_npimage(im)