用python演奏歌曲

一、准备

电脑一台、python。

首先使用pip安装winsound:

pip install winsound

接下来,就开始我们的正文。(当然,这篇文章正文会很短)

二、正文

此处,我以《平凡之路》为例

因为全写成winsound.Beep(262,m)的形式很容易错,所以我在这里首先规定了一些函数,后面直接调用函数。

def do(m):

winsound.Beep(262,m)

def re(m):

winsound.Beep(294,m)

def mi(m):

winsound.Beep(330,m)

def fa(m):

winsound.Beep(349,m)

def so(m):

winsound.Beep(392,m)

def la(m):

winsound.Beep(440,m)

def si(m):

winsound.Beep(494,m)

def do_(m):

winsound.Beep(523,m)

def re_(m):

winsound.Beep(565,m)

def n(m):

time.sleep(m/1000) #这个是0

然后,就是无休止的肝了。

在此,我为了省一些事,就把m(毫秒数)规定为了3个变量,便于码字。

三、全部代码

import time
import winsound

a = 732
b = 366
c = 244

def do(m):
    winsound.Beep(262,m)
def re(m):
    winsound.Beep(294,m)
def mi(m):
    winsound.Beep(330,m)
def fa(m):
    winsound.Beep(349,m)
def so(m):
    winsound.Beep(392,m)
def la(m):
    winsound.Beep(440,m)
def si(m):
    winsound.Beep(494,m)
def do_(m):
    winsound.Beep(523,m)
def re_(m):
    winsound.Beep(578,m)
def n(m):
    time.sleep(m/1000)#0

#正文
print("平凡之路")

la(a)
mi(b)
do(b)
fa(b)
la(b)
fa(b)
do(b)
do(b)
so(b)
mi(b)
do(b)
si(b)
do(b)
re(b)
so(b)
la(a)
mi(b)
do(b)
fa(b)
la(b)
fa(b)
do(b)
do(b)
so(b)
mi(b)
do(b)
si(b)
do(b)
re(b)
so(b)
n(b)
mi(b)
mi(c)
la(c)
la(c)
la(b)
do(b)
re(c)
mi(c+c+a+a+a)
n(a)
n(b)
mi(b)
mi(c)
la(c*2)
la(b)
so(b)
so(b)
fa(b)
mi(a*3)
n(a)
n(b)
mi(b)
mi(b)
la(b*2)
do(b)
re(c)
mi(c*2)
mi(a*3)
n(a)
mi(b*2)
do(b)
fa(c*3)
fa(b)
mi(b)
do(a*3)
n(a)
n(b)
mi(b)
mi(c)
la(c)
la(c)
la(b)
do(b)
re(c)
mi(c)
mi(c)
mi(a*3)
n(a)
n(b)
mi(b)
mi(c)
la(c*2)
la(b)
so(b)
so(b)
fa(b)
mi(a*3)
n(a)
n(b)
mi(b)
mi(b)
la(b*2)
do(b)
re(c)
mi(c*2)
mi(a*3)
n(a)
mi(b*2)
do(b)
fa(c*3)
fa(b)
mi(b)
do(a*2)
n(b)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c)
la(a)
so(c)
fa(c)
mi(c+a+c)
fa(c)
re(c+b)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c+a+c)
si(c)
do_(c+a+c+c)
re_(c)
do_(c+a)
si(c)
do_(c)
re_(c+a)
do_(c)
si(c)
do_(c+a+a)
n(b)
mi(b)
mi(c)
la(c)
la(c)
la(b)
do(b)
re(c)
mi(c)
mi(c)
mi(a*3)
n(a)
n(b)
mi(b)
mi(c)
la(c*2)
la(b)
so(b)
so(b)
fa(b)
mi(a*3)
n(a)
n(b)
mi(b)
mi(b)
la(b*2)
do(b)
re(c)
mi(c*2)
mi(a*3)
n(a)
mi(b*2)
do(b)
fa(c*3)
fa(b)
mi(b)
do(a*2)
n(b)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c)
la(a)
so(c)
fa(c)
mi(c+a+c)
fa(c)
re(c+b)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c+a+c)
si(c)
do_(c+a+c+c)
re_(c)
do_(c+a)
si(c)
do_(c)
re_(c+a)
do_(c)
si(c)
do_(c+a+a)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c)
la(a)
so(c)
fa(c)
mi(c+a+c)
fa(c)
re(c+b)
so(b)
la(b)
si(b)
do_(c)
si(c)
do_(c*2)
so(c)
la(c+a+c)
si(c)
do_(c+a+c+c)
re_(c)
do_(c+a)
si(c)
do_(c)
re_(c+a)
do_(c)
si(c)
do_(c+a+a)
n(b)
mi(b)
mi(c)
la(c)
la(c)
la(b)
do(b)
re(c)
mi(c)
mi(c)
mi(a*3)
n(a)
n(b)
mi(b)
mi(c)
la(c*2)
la(b)
so(b)
so(b)
fa(b)
mi(a*3)
n(a)
n(b)
mi(b)
mi(b)
la(b*2)
do(b)
re(c)
mi(c*2)
mi(a*3)
n(a)
mi(b*2)
do(b)
fa(c*3)
fa(b)
mi(b)
do(a*3)
n(a)

四、最后

送上Beep的频率对照表:

qdo 262

qre 294

qmi 330

qfa 349

qso 392

qla 440

qsi 494

do 523

re 578

mi 659

fa 698

so 784

la 880

si 988

do1 1046

re1 1175

mi1 1318

fa1 1480

so1 1568

la1 1760

si1 1976

sqdo 277

sqre 311

sqfa 370

sqso 415

sqla 466

sdo 554

sre 622

sfa 740

sso 831

sla 932

sdo1 1046

sre1 1245

sfa1 1480

sso1 1661

sla1 1865

如果您也想做一个,可以使用我这里的频率对照表进行调整。

你可能感兴趣的:(python,python)