如何同时在python上播放两个不同的歌曲文件? [英] How do you play two different song files on python at the same time?

查看:111
本文介绍了如何同时在python上播放两个不同的歌曲文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时播放song1和song2(已注释掉的一首),但我不知道如何使用myro或winsound或其他方法来播放.有人建议使用线程,但是我也不知道该怎么做,因为我只是一个非常初级的程序员.有人可以帮我解决这个问题和/或详细说明如何执行此操作吗?这是我到目前为止的内容:

I'm trying to play song1 and song2 (the commented out one) at the same time, but I don't know how to do that using myro or winsound or whatever it is. Someone suggested using threading, but I'm not sure how to do that either, since I am just a very baby beginner programmer. Would someone please help me figure this thing out and/or explain in detail how to do this? Here's what I have so far:

import winsound
from myro import *
def main():

    HftM1 = makeSong("Db4 3/8; C4 3/8; Bb3 1/4; Bb3 3/8; Ab3 3/8; Gb3 3/8; Ab3 1/16; C4 3/8; Bb3 3/8; Ab3 1/8; Eb3 1/16; F3 1/16; Ab3 3/8; G3 3/8; F3 3/8; C4 1/16; Db4 3/8; C4 3/8; Bb3 1/8; F3 1/16; Gb3 1/16; Bb3 3/8; Ab3 3/8; Gb3 3/8; C4 3/8; Bb3 3/8; Ab3 1/8; Eb3 1/16; F3 1/16; Ab3 3/8; G3 3/8; F3 3/8")

    saveSong(HftM1, "HymnfortheMissing1.txt", append=1)

    HftM2 = makeSong("Bb2 1/8; F3 1/8; Bb3 1/4; Bb2 1/8; F3 3/8; Gb2 1/8; Db3 1/8; Gb3 1/4; Gb2 1/8; Db3 3/8; Ab2 1/8; Eb3 1/8; Ab3 1/4; Ab2 1/8; Eb3 3/8; F2 1/8; C3 1/8; F3 1/4; F2 1/8; C3 3/8; Bb2 1/8; F3 1/8; Bb3 1/4; Bb2 1/8; F3 3/8; Gb2 1/8; Db3 1/8; Gb3 1/4; Gb2 1/8; Db3 3/8; Ab2 1/8; Eb3 1/8; Ab3 1/4; Ab2 1/8; Eb3 3/8; F2 1/8; C3 1/8; F3 1/4; F2 1/8; C3 3/8")

    saveSong(HftM2, "HymnfortheMissing2.txt", append=1)

    song1 = readSong("HymnfortheMissing1.txt")

    #song2 = readSong("HymnfortheMissing2.txt") #This part of the song is supposed to be played at the same time as the first part, but I don't know how to do that so...

    play = []

    for n in range(len(song1)):
        play = song1[n]
        note = play[0]
        duration = play[1]
        winsound.Beep(int(note), int(duration*2000))
main()

推荐答案

我建议您使用 PyGame 进行此类工作,并查看

I recommend you use PyGame for this kind of work, and check out this related question. You'd have to use the mixer module.

通常,您必须对音频流使用某种数学操作来混合PCM数据(例如,示例讨论

In general you would have to mix the PCM data using some mathematical manipulation on the audio streams (for an example discussion see this question).

更新:Myro(我从未使用过)在文档中说 使用beep函数可以做到这一点:

Update: Myro (which I have never used) says in the docs that there is a way to do this using the beep function:

提示音(自己,持续时间,频率,频率2 =无):发出提示音.如果 给出了两种音调,机器人会将它们组合.

beep(self, duration, frequency, frequency2 = None): make a tone. If two tones are given, the robot will combine them.

举一个简单的例子,如果您在第一首歌曲中具有音符A(持续时间为1秒),在第二首歌曲中具有音符B(持续时间为2秒),则您必须调用beep两次:首先同时演奏音符A和B同时播放1秒钟,然后仅播放音符B持续1秒钟.

As a simple example, if you have note A, duration 1 second in the first song, and note B, duration 2 seconds in the second song you will have to call beep twice: First to play both notes A and B simultaneously for 1 second, and then to play only note B for another 1 second.

不用说,这段代码比使用PyGame之类的音频文件播放声音并进行混合要复杂得多.

Needless to say, this code will be much more complicated than just playing sounds from audio files using something like PyGame and mixing them.

这篇关于如何同时在python上播放两个不同的歌曲文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆