我很困惑,因为有很多程序。但我看起来像这样。我会输入一个像“a4 c3 h3 a2”等旋律然后我想听到这个。有人知道我在找什么吗? 提前谢谢
答案 0 :(得分:7)
从音符名称计算频率很容易。每个半音符距前一音符2 ^(1/12),440 Hz为A4。
如果你有机会在Windows上,你可以尝试这段代码,通过PC扬声器播放一首歌:
import math
import winsound
import time
labels = ['a','a#','b','c','c#','d','d#','e','f','f#','g','g#']
# name is the complete name of a note (label + octave). the parameter
# n is the number of half-tone from A4 (e.g. D#1 is -42, A3 is -12, A5 is 12)
name = lambda n: labels[n%len(labels)] + str(int((n+(9+4*12))/12))
# the frequency of a note. the parameter n is the number of half-tones
# from a4, which has a frequency of 440Hz, and is our reference note.
freq = lambda n: int(440*(math.pow(2,1/12)**n))
# a dictionnary associating note frequencies to note names
notes = {name(n): freq(n) for n in range(-42,60)}
# the period expressed in second, computed from a tempo in bpm
period = lambda tempo: 1/(tempo/60)
# play each note in sequence through the PC speaker at the given tempo
def play(song, tempo):
for note in song.lower().split():
if note in notes.keys():
winsound.Beep(notes[note], int(period(tempo)*1000))
else:
time.sleep(period(tempo))
# "au clair de la lune"!! 'r' is a rest
play( 'c4 c4 C4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r '
'c4 C4 c4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r '
'd4 d4 d4 d4 A3 r a3 r d4 c4 B3 a3 g3 r r r '
'c4 c4 c4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r ', 180 )
(请注意我使用的是python 3.x,您可能需要调整代码的某些部分才能在python 2.x上使用它。)
顺便说一句,我使用abcdefg
作为比例,但您肯定会找到使用h
代替b
的方法。
答案 1 :(得分:3)
一个外部选项JFugue为shown here(使用Groovy)。请注意,您将使用Jython而不是Python,这有望作为答案在范围内。
答案 2 :(得分:2)
你可以使用任何产生MIDI输出的库,如果是.net我建议 来自微软的Stephen Toub创建的那个(找不到我从哪里得到它,但google就可以找到它。)
答案 3 :(得分:1)
检查一下:http://www.algorithm.co.il/blogs/index.php/pytuner/ 这是一个非常相似的项目,看起来是一个非常不错的参考。
答案 4 :(得分:1)
这是与@Adrien Plisson 相同的程序,但适用于 macOS:
import math
import time
import pygame
import numpy
sampleRate = 44100
freq = 440
labels = ['a','a#','b','c','c#','d','d#','e','f','f#','g','g#']
name = lambda n: labels[n%len(labels)] + str(int((n+(9+4*12))/12))
# name is the complete name of a note (label + octave).
# the parameter n is the number of half-tone from A4 (e.g. D#1 is -42, A3 is -12, A5 is 12)
freq = lambda n: int(440*(math.pow(2,1/12)**n))
# the frequency of a note.
# the parameter n is the number of half-tones from a4, which has a frequency of 440Hz, and is our reference note.
notes = {name(n): freq(n) for n in range(-42,60)}
# a dictionnary associating note frequencies to note names
period = lambda tempo: 1/(tempo/60)
# the period expressed in second, computed from a tempo in bpm
def play(song, tempo):
for note in song.lower().split():
if note in notes.keys():
pygame.mixer.init(notes[note])
arr = numpy.array([4096 * numpy.sin(2.0 * numpy.pi * notes[note] * x / sampleRate) for x in range(0, sampleRate)]).astype(numpy.int16)
arr2 = numpy.c_[arr,arr]
sound = pygame.sndarray.make_sound(arr2)
sound.play(-1)
pygame.time.delay(1000)
sound.stop()
else:
time.sleep(period(tempo))
# play each note in sequence through the PC speaker at the given tempo
play( 'c4 c4 C4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r '
'c4 C4 c4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r '
'd4 d4 d4 d4 A3 r a3 r d4 c4 B3 a3 g3 r r r '
'c4 c4 c4 d4 e4 r d4 r c4 e4 d4 d4 c4 r r r ', 180 )
# "au clair de la lune"!! 'r' is a rest