首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从使用Music21制作的音乐中生成实时音频输出?

如何从使用Music21制作的音乐中生成实时音频输出?
EN

Stack Overflow用户
提问于 2012-06-11 23:48:24
回答 2查看 4.3K关注 0票数 6

如何从使用Music21制作的音乐中生成实时音频输出。如果做不到这一点,我如何通过开源软件从用Music21制作的音乐中产生任何音频输出?谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-13 03:44:28

这就是我所发现的。这是一个在Windows XP上工作的python脚本。除了music21,它还需要pygame。

代码语言:javascript
复制
# genPlayM21Score.py Generates and Plays 2 Music21 Scores "on the fly".
#
# see way below for source notes

from music21 import *

# we create the music21 Bottom Part, and do this explicitly, one object at a time.

n1 = note.Note('e4')
n1.duration.type = 'whole'
n2 = note.Note('d4')
n2.duration.type = 'whole'
m1 = stream.Measure()
m2 = stream.Measure()
m1.append(n1)
m2.append(n2)
partLower = stream.Part()
partLower.append(m1)
partLower.append(m2)

# For the music21 Upper Part, we automate the note creation procedure

data1 = [('g4', 'quarter'), ('a4', 'quarter'), ('b4', 'quarter'), ('c#5', 'quarter')]
data2 = [('d5', 'whole')]
data = [data1, data2]
partUpper = stream.Part()

def makeUpperPart(data):
    for mData in data:
        m = stream.Measure()
        for pitchName, durType in mData:
            n = note.Note(pitchName)
            n.duration.type = durType
            m.append(n)
        partUpper.append(m)
makeUpperPart(data)        

# Now, we can add both Part objects into a music21 Score object.  

sCadence = stream.Score()
sCadence.insert(0, partUpper)
sCadence.insert(0, partLower)

# Now, let's play the MIDI of the sCadence Score [from memory, ie no file  write necessary] using pygame

import cStringIO

# for music21 <= v.1.2:
if hasattr(sCadence, 'midiFile'):
   sCadence_mf = sCadence.midiFile
else: # for >= v.1.3:
   sCadence_mf = midi.translate.streamToMidiFile(sCadence)
sCadence_mStr = sCadence_mf.writestr()
sCadence_mStrFile = cStringIO.StringIO(sCadence_mStr)

import pygame

freq = 44100    # audio CD quality
bitsize = -16   # unsigned 16 bit
channels = 2    # 1 is mono, 2 is stereo
buffer = 1024    # number of samples
pygame.mixer.init(freq, bitsize, channels, buffer)

# optional volume 0 to 1.0
pygame.mixer.music.set_volume(0.8)

def play_music(music_file):
    """
    stream music with mixer.music module in blocking manner
    this will stream the sound from disk while playing
    """
    clock = pygame.time.Clock()
    try:
        pygame.mixer.music.load(music_file)
        print "Music file %s loaded!" % music_file
    except pygame.error:
        print "File %s not found! (%s)" % (music_file, pygame.get_error())
        return
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        # check if playback has finished
        clock.tick(30)

# play the midi file we just saved
play_music(sCadence_mStrFile)

#============================

# now let's make a new music21 Score by reversing the upperPart notes
data1.reverse()
data2 = [('d5', 'whole')]
data = [data1, data2]
partUpper = stream.Part()
makeUpperPart(data)        
sCadence2 = stream.Score()
sCadence2.insert(0, partUpper)
sCadence2.insert(0, partLower)

# now let's play the new Score
sCadence2_mf = sCadence2.midiFile
sCadence2_mStr = sCadence2_mf.writestr()
sCadence2_mStrFile = cStringIO.StringIO(sCadence2_mStr)
play_music(sCadence2_mStrFile)



## SOURCE NOTES
## There are 3 sources for this mashup:

# 1. Source for the Music21 Score Creation http://web.mit.edu/music21/doc/html/quickStart.html#creating-notes-measures-parts-and-scores

# 2.  Source for the Music21 MidiFile Class Behaviour http://mit.edu/music21/doc/html/moduleMidiBase.html?highlight=midifile#music21.midi.base.MidiFile

# 3.  Source for the pygame player: http://www.daniweb.com/software-development/python/code/216979/embed-and-play-midi-music-in-your-code-python
票数 7
EN

Stack Overflow用户

发布于 2012-06-27 13:34:29

正如您所看到的,music21的设计初衷不是作为一个音乐回放系统,而是将其嵌入到其他回放系统中,或者从系统内部调用它们。我们不打算在回放系统中投入太多的工作(因为硬件支持,我们是一个很小的研究实验室,音乐分析方面的工作仍然需要完成,等等),但你的解决方案是如此优雅,现在它作为music21.midi.realtime模块包含在所有版本的music21 (发布v1.1)中。下面是一个例子,它以Music21的能力为不同的音高弯曲对象动态分配midi通道,以模拟微音播放(这是大多数midi播放的主要问题):

代码语言:javascript
复制
#  Set up a detuned piano 
#  (where each key has a random 
#  but consistent detuning from 30 cents flat to sharp)
#  and play a Bach Chorale on it in real time.

from music21 import *
import random
keyDetune = []
for i in range(0, 127):
    keyDetune.append(random.randint(-30, 30))

b = corpus.parse('bach/bwv66.6')
for n in b.flat.notes:
    n.microtone = keyDetune[n.midi]
sp = midi.realtime.StreamPlayer(b)
sp.play()

StreamPlayer的.play()函数还可以接受busyFunction、busyArgs和busyWaitMilliseconds参数,这些参数指定了一个函数,最多只能在每个busyWaitMilliseconds中使用参数调用(如果系统速度较慢,调用的函数可能会更多)。还有一个endFunction和endArgs将在结束时被调用,以防您想要设置某种线程回放。-- Myke Cuthbert (Music21创建者)

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10983462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档