鱼C论坛

 找回密码
 立即注册
查看: 7690|回复: 11

[Pygame] mixer | Pygame 中文文档

[复制链接]
发表于 2015-6-19 00:01:44 | 显示全部楼层 |阅读模式
购买主题 已有 12 人购买  本主题需向作者支付 10 鱼币 才能浏览
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-3-19 21:10:02 | 显示全部楼层
支持楼主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-8 13:51:31 | 显示全部楼层
楼主能快点填么?刚好要看这个,居然木有。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-10 10:28:55 | 显示全部楼层
小甲鱼快快啊!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-31 11:38:32 | 显示全部楼层
填坑,不填我就绿了你女朋友
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-1 16:14:06 | 显示全部楼层
甲鱼兄快点填啊,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-19 23:46:58 | 显示全部楼层
看到还没有翻译,于是自己动手翻译了一下以飨大家,希望能有点用途。不过由于我水平和知识面有限,错误和纰漏的地方在所难免,希望大家批评指正。


pygame.mixer
pygame用来加载和播放声音的模块
pygame.mixer.init  —        初始化mixer 模块
pygame.mixer.pre_init  —        预设mixer.init()函数的参数
pygame.mixer.quit  —        取消对mixer的初始化
pygame.mixer.get_init   —        测试mixer是否已经初始化
pygame.mixer.stop —        关闭所有音频的播放
pygame.mixer.pause   —        暂停所有音频的播放
pygame.mixer.unpause —        取消所有音频播放的暂停
pygame.mixer.fadeout  —        所有声音停止前音量逐步降低
pygame.mixer.set_num_channels  —        设置播放频道的总数
pygame.mixer.get_num_channels   —        获取播放频道的总数
pygame.mixer.set_reserved  —        保留频道不被自动占用
pygame.mixer.find_channel  —        查找一个未使用的频道
pygame.mixer.get_busy —        测试任何声音是否被混合
pygame.mixer.Sound    —        从文件或缓冲对象创建一个新的Sound对象
pygame.mixer.Channel  —        创建一个频道对象以控制播放

This module contains classes for loading Sound objects and controlling playback. The mixer module is optional and depends on SDL_mixer. Your program should test that pygame.mixer is available and intialized before using it.
本模块包含加载声音对象和控制播放的类。mixer模块是可选的且基于SDL_mixer。你的程序在使用pygame.mixer前需测试该模块是否存在并对其进行初始化。
The mixer module has a limited number of channels for playback of sounds. Usually programs tell pygame to start playing audio and it selects an available channel automatically. The default is 8 simultaneous channels, but complex programs can get more precise control over the number of channels and their use.
mixer模块播放声音时使用的频道数是有限的。通常程序告知pygame开始播放音频并自动选取一个可用的频道。尽管默认的同时播放的通道数为8,但复杂的程序可以更精确的控制频道的数目及其用途。
All sound playback is mixed in background threads. When you begin to play a Sound object, it will return immediately while the sound continues to play. A single Sound object can also be actively played back multiple times.
所有的声音播放在后台进程中混合。打你开始播放一个Sound对象时,该对象会立即返回而声音继续播放。单个Sound对象也可以播放多次。
The mixer also has a special streaming channel. This is for music playback and is accessed through the pygame.mixer.music module.
混合器还有一个特殊的流频道,用于音乐的播放并且通过pygame.mixer.music模块可以获取。
The mixer module must be initialized like other pygame modules, but it has some extra conditions. The pygame.mixer.init() function takes several optional arguments to control the playback rate and sample size. Pygame will default to reasonable values, but pygame cannot perform Sound resampling, so the mixer should be initialized to match the values of your audio resources.
虽然mixer模块也必须像其他模块一样先进行初始化,但还需要额外的条件。pygame.mixer.init()函数使用几个可选的参数来控制播放速率和样本尺寸。Pygame选取了适当的数值作为其默认值,但pygame不能对声音重采样,因而mixer需要初始化为适合你的音频源的数值。
NOTE: Not to get less laggy sound, use a smaller buffer size. The default is set to reduce the chance of scratchy sounds on some computers. You can change the default buffer by calling pygame.mixer.pre_init()preset the mixer init arguments before pygame.mixer.init()initialize the mixer module or pygame.init()initialize all imported pygame modules is called. For example: pygame.mixer.pre_init(44100,-16,2, 1024) The default size was changed from 1024 to 3072 in pygame 1.8.
注:使用较小的buffer数值来减小声音延迟。默认值可以减少某些电脑上沙沙声发生的机会。你可以通过在调用pygame.mixer.init()或pygame.init()前调用pygame.mixer.pre_init()来改变默认的buffer值。例如,pygame.mixer.pre_init(44100,-16,2, 1024)。在pygame 1.8中默认buffer参数的数值可从1024变化到3072。

pygame.mixer.init()
initialize the mixer module
init(frequency=22050, size=-16, channels=2, buffer=4096) -> None
Initialize the mixer module for Sound loading and playback. The default arguments can be overridden to provide specific audio mixing. Keyword arguments are accepted. For backward compatibility where an argument is set zero the default value is used (possible changed by a pre_init call).

初始化mixer模块
init(frequency=22050, size=-16, channels=2, buffer=4096) -> None
初始化用于加载声音对象和控制播放的mixer模块。为了对特定音频进行混合,默认参数可被改写。也可以接受关键字参数。为便于向后兼容,使用0作为参数的默认值(可以通过调用pre_init函数来改变)。
The size argument represents how many bits are used for each audio sample. If the value is negative then signed sample values will be used. Positive values mean unsigned audio samples will be used. An invalid value raises an exception.
The channels argument is used to specify whether to use mono or stereo. 1 for mono and 2 for stereo. No other values are supported (negative values are treated as 1, values greater than 2 as 2).
Size参数用于指定使用的是单音还是立体音。1代表单音,2代表立体音。不支持其他数值(负数会被当做1来处理,而大于2的数值将视为2)。
The buffer argument controls the number of internal samples used in the sound mixer. The default value should work for most cases. It can be lowered to reduce latency, but sound dropout may occur. It can be raised to larger values to ensure playback never skips, but it will impose latency on sound playback. The buffer size must be a power of two (if not it is rounded up to the next nearest power of 2).
Buffer参数控制声音混合器使用的内部样本的数量。默认值对大部分情况都是有效的。虽然减小其数值可以降低延迟,但可能会导致声音丢失。较大的数值尽管可以保证播放不会跳过,但会对声音播放造成延迟。
Some platforms require the pygame.mixer module to be initialized after the display modules have initialized. The top level pygame.init() takes care of this automatically, but cannot pass any arguments to the mixer init. To solve this, mixer has a function pygame.mixer.pre_init() to set the proper defaults before the toplevel init is used.
It is safe to call this more than once, but after the mixer is initialized you cannot change the playback arguments without first calling pygame.mixer.quit().
一些平台要求pygame.mixer在display模块初始化后必须进行初始化。虽然顶层的pygame.init()会自动进行这些操作,但它不能传递任何参数到mixer init。为解决这个问题,在顶层初始化后可以使用函数pygame.mixer.pre_init()来设置合适的默认值。
尽管可以不止一次的调用这个函数,但是如果不事先调用pygame.mixer.quit()的情况下一旦混合器被初始化了你就无法改变播放参数。

pygame.mixer.pre_init()
preset the mixer init arguments
pre_init(frequency=22050, size=-16, channels=2, buffersize=4096) -> None
Call pre_init to change the defaults used when the real pygame.mixer.init() is called. Keyword arguments are accepted. The best way to set custom mixer playback values is to call pygame.mixer.pre_init() before calling the top level pygame.init(). For backward compatibility argument values of zero is replaced with the startup defaults.

预设混合器init函数的参数
pre_init(frequency=22050, size=-16, channels=2, buffersize=4096) -> None
当pygame.mixer.init()被调用时调用pre_init函数可以改变默认值。接受关键字参数。设置自定义播放数值的最佳方式是在调用顶层pygame.init()之前调用pygame.mixer.pre_init()。为便于向后兼容,0值的参数被启动时的默认值代替。

pygame.mixer.quit()
uninitialize the mixer
quit() -> None
This will uninitialize pygame.mixerpygame module for loading and playing sounds. All playback will stop and any loaded Sound objects may not be compatible with the mixer if it is reinitialized later.
取消对mixer的初始化
quit() -> None
该函数将取消对pygame.mixer的初始化。所有的播放将会停止且任何已加载的Sound对象后续如果重新初始化时会与混合器不兼容

pygame.mixer.get_init()
test if the mixer is initialized
get_init() -> (frequency, format, channels)
If the mixer is initialized, this returns the playback arguments it is using. If the mixer has not been initialized this returns None
测试混合器是否已初始化
get_init() -> (frequency, format, channels)
如果混合器初始化了,将会返回使用的播放参数。如果混合器还没有初始化,将会返回None。

pygame.mixer.stop()
stop playback of all sound channels
stop() -> None
This will stop all playback of all active mixer channels.
停止所有音频的播放
stop() -> None
将会停止所有激活的混合器频道的所有播放。

pygame.mixer.pause()
temporarily stop playback of all sound channels
pause() -> None
This will temporarily stop all playback on the active mixer channels. The playback can later be resumed with pygame.mixer.unpause()
暂停所有音频的播放
pause() -> None
将会暂停所有在激活的混合器频道上的播放。之后可以通过pygame.mixer.unpause()来恢复播放。

pygame.mixer.unpause()
resume paused playback of sound channels
unpause() -> None
This will resume all active sound channels after they have been paused.
恢复音频的播放
unpause() -> None
在暂停音频后可将所有激活的音频恢复播放。

pygame.mixer.fadeout()
fade out the volume on all sounds before stopping
fadeout(time) -> None
This will fade out the volume on all active channels over the time argument in milliseconds. After the sound is muted the playback will stop.
所有声音停止前音量逐步降低
fadeout(time) -> None
以毫秒为单位随时间逐步降低所有激活的频道的音量。声音变弱后播放将会停止。

pygame.mixer.set_num_channels()
set the total number of playback channels
set_num_channels(count) -> None
Sets the number of available channels for the mixer. The default value is 8. The value can be increased or decreased. If the value is decreased, sounds playing on the truncated channels are stopped.
设置播放频道的总数
set_num_channels(count) -> None
设置混合器可选频道的数目。默认值为8且可被增大或减小。如果数值减小了,截断通道上播放的声音停止播放。

pygame.mixer.get_num_channels()
get the total number of playback channels
get_num_channels() -> count
Returns the number of currently active playback channels.
获取播放频道的总数
get_num_channels() -> count
返回当前活跃的播放频道的数目。

pygame.mixer.set_reserved()
reserve channels from being automatically used
set_reserved(count) -> None
The mixer can reserve any number of channels that will not be automatically selected for playback by Sounds. If sounds are currently playing on the reserved channels they will not be stopped.
This allows the application to reserve a specific number of channels for important sounds that must not be dropped or have a guaranteed channel to play on.
保留频道不被自动占用
set_reserved(count) -> None
混合器可以保留任意数量的频道以便不被Sound自动选用。当前在保留频道上播放的sounds不好被停止。
这样保证了应用为那些确实不可以被丢弃的重要声音保留一定数量的频道或者确保有一频道可供播放。

pygame.mixer.find_channel()
find an unused channel
find_channel(force=False) -> Channel
This will find and return an inactive Channel object. If there are no inactive Channels this function will return None. If there are no inactive channels and the force argument is True, this will find the Channel with the longest running Sound and return it.
If the mixer has reserved channels from pygame.mixer.set_reserved() then those channels will not be returned here.
查找一个为被使用的频道
find_channel(force=False) -> Channel
查找并返回一个未被激活的Channel对象。如果没有未被激活的Channel对象将返回None。如果没有未被激活的Channel对象且force参数为True,将查找带有最长运行Sound的Channel并将其返回。
如果混合器通过pygame.mixer.set_reserved()设置了保留频道,那么这些Channel不会被返回。

pygame.mixer.get_busy()
test if any sound is being mixed
get_busy() -> bool
Returns True if the mixer is busy mixing any channels. If the mixer is idle then this return False.
测试任意声音是否被混合
get_busy() -> bool
如果混合器正忙于混合频道则返回True。如果混合器空闲则返回False。


pygame.mixer.Sound
Create a new Sound object from a file or buffer object
从文件或buffer对象创建一个新的Sound对象
Sound(filename) -> Sound
Sound(file=filename) -> Sound
Sound(buffer) -> Sound
Sound(buffer=buffer) -> Sound
Sound(object) -> Sound
Sound(file=object) -> Sound
Sound(array=object) -> Sound

pygame.mixer.Sound.play    —        开始播放声音
pygame.mixer.Sound.stop   —        停止播放声音
pygame.mixer.Sound.fadeout —        在声音渐弱后停止播放
pygame.mixer.Sound.set_volume  —        设置Sound对象播放的音量
pygame.mixer.Sound.get_volume  —        获取播放的音量
pygame.mixer.Sound.get_num_channels  —        记录Sound播放的次数
pygame.mixer.Sound.get_length   —        获取Sound的长度
pygame.mixer.Sound.get_raw —        返回Sound样本的字节串副本

Load a new sound buffer from a filename, a python file object or a readable buffer object. Limited resampling will be performed to help the sample match the initialize arguments for the mixer. A Unicode string can only be a file pathname. A Python 2.x string or a Python 3.x bytes object can be either a pathname or a buffer object. Use the ‘file’ or ‘buffer’ keywords to avoid ambiguity; otherwise Sound may guess wrong. If the array keyword is used, the object is expected to export a version 3, C level array interface or, for Python 2.6 or later, a new buffer interface (The object is checked for a buffer interface first.)
The Sound object represents actual sound sample data. Methods that change the state of the Sound object will the all instances of the Sound playback. A Sound object also exports an array interface, and, for Python 2.6 or later, a new buffer interface.
The Sound can be loaded from an OGG audio file or from an uncompressed WAV.
Note: The buffer will be copied internally, no data will be shared between it and the Sound object.
从文件名、python文件对象或者是可读的buffer对象加载一个新的声音缓冲。为便于样本匹配混合器的初始参数会进行有限的重采样。Unicode字符串仅可作为文件路径名。Python 2.x string或者 Python 3.x bytes对象即可作为路径名又可作为一个buffer对象。使用’file’或者‘buffer’关键字来避免歧义,否则Sound会判断失误。如果使用’array’关键字,该对象被视为导出一个Python 3的C层面的array界面,或者对于Python 2.6及之后版本则视为一个新的buffer界面。
Sound可从一个OGG音频文件或者未压缩的WAV文件加载。
注:buffer的拷贝发生在内部,不会与Sound对象共享数据。
For now buffer and array support is consistent with sndarray.make_sound for Numeric arrays, in that sample sign and byte order are ignored. This will change, either by correctly handling sign and byte order, or by raising an exception when different. Also, source samples are truncated to fit the audio sample size. This will not change.
pygame.mixer.Sound(buffer) is new in pygame 1.8 pygame.mixer.SoundCreate a new Sound object from a file or buffer object keyword arguments and array interface support new in pygame 1.9.2
目前,buffer和array与sndarray.make_sound对于数值阵列是一致的,都会忽略样本的标记和字节顺序。既可以通过适当处理标记和字节顺序,也可以通过当不同时则抛出异常的方法来改变。同时,源样本会被截断以便适合音频样本尺寸。这一点不会改变。
pygame.mixer.Sound(buffer)是pygame 1.8新增的内容。Pygame.mixer.Sound关键字参数和array界面支持为pygame 1.9.2新增的内容。

play()
begin sound playback
play(loops=0, maxtime=0, fade_ms=0) -> Channel
Begin playback of the Sound (i.e., on the computer’s speakers) on an available Channel. This will forcibly select a Channel, so playback may cut off a currently playing sound if necessary.
开始播放声音
play(loops=0, maxtime=0, fade_ms=0) -> Channel
开始在一个可用的频道上播放声音(例如在一台电脑的喇叭上)。这将强制选取一个Channel,因而播放时如果必要时会砍掉一个当前正在播放的声音。
The loops argument controls how many times the sample will be repeated after being played the first time. A value of 5 means that the sound will be played once, then repeated five times, and so is played a total of six times. The default value (zero) means the Sound is not repeated, and so is only played once. If loops is set to -1 the Sound will loop indefinitely (though you can still call stop() to stop it).
The maxtime argument can be used to stop playback after a given number of milliseconds.
The fade_ms argument will make the sound start playing at 0 volume and fade up to full volume over the time given. The sample may end before the fade-in is complete.
This returns the Channel object for the channel that was selected.
loops参数控制样本从第一次播放开始算起重复的次数。数值5意味着这个声音要被播放一次,然后重复5次,因而总计播放了6次。默认值(0)意味着Sound对象不被重复,亦即仅播放一次。如果将loops设置为-1,那么Sound对象会无限制地循环播放(不过你仍旧可以调用stop()来停止它)。

stop()
stop sound playback
stop() -> None
This will stop the playback of this Sound on any active Channels.
停止声音的播放
stop() -> None
将会停止该Sound对象在任何激活的Channel上的播放。

fadeout()
stop sound playback after fading out
fadeout(time) -> None
This will stop playback of the sound after fading it out over the time argument in milliseconds. The Sound will fade and stop on all actively playing channels.
在声音渐弱后停止播放
fadeout(time) -> None
将会在声音渐弱后以毫秒为time参数的单位来停止播放。Sound将会渐弱并在所有活跃播放的channel上停止。

set_volume()
set the playback volume for this Sound
set_volume(value) -> None
This will set the playback volume (loudness) for this Sound. This will immediately affect the Sound if it is playing. It will also affect any future playback of this Sound. The argument is a value from 0.0 to 1.0.
设置该Sound播放的音量
set_volume(value) -> None
将会设置该Sound播放的音量(响度)。如果声音在播放,这将立马见效。这也将会影响之后该Sound的任何播放。value参数的值从0.0到1.0。

get_volume()
get the playback volume
get_volume() -> value
Return a value from 0.0 to 1.0 representing the volume for this Sound.
获取播放的音量
get_volume() -> value
返回一个从0.0到1.0代表该Sound音量值的数值。

get_num_channels()
count how many times this Sound is playing
get_num_channels() -> count
Return the number of active channels this sound is playing on.
记录Sound播放的次数
get_num_channels() -> count
返回供该声音播放的活跃频道数目。

get_length()
get the length of the Sound
get_length() -> seconds
Return the length of this Sound in seconds.
获取Sound的长度
get_length() -> seconds
返回以秒计时的该Sound的长度。

get_raw()
return a bytestring copy of the Sound samples.
get_raw() -> bytes
Return a copy of the Sound object buffer as a bytes (for Python 3.x) or str (for Python 2.x) object.
New in pygame 1.9.2.
返回Sound样本的字节串副本
get_raw() -> bytes
返回该Sound对象缓冲作为一个bytes对象(Python 3.x) 或者是str对象(Python 2.x)的副本。


pygame.mixer.Channel
Create a Channel object for controlling playback
创建一个Channel对象来控制播放
Channel(id) -> Channel

pygame.mixer.Channel.play  —        在指定Channel上播放声音
pygame.mixer.Channel.stop  —        停止一个频道上的播放
pygame.mixer.Channel.pause —        暂停一个频道上的播放
pygame.mixer.Channel.unpause   —        取消暂停一个频道上的播放
pygame.mixer.Channel.fadeout    —        频道渐弱后停止播放
pygame.mixer.Channel.set_volume  —        设置一个正在播放的频道的音量
pygame.mixer.Channel.get_volume  —        获取一个正在播放的频道的音量
pygame.mixer.Channel.get_busy   —        检查该频道是否活跃
pygame.mixer.Channel.get_sound  —        获取当前正在播放的Sound
pygame.mixer.Channel.queue —        使一个Sound对象紧接当前排队
pygame.mixer.Channel.get_queue  —        返回任何在排队中的Sound
pygame.mixer.Channel.set_endevent   —        当播放停止时使频道引发一个事件
pygame.mixer.Channel.get_endevent   —        当播放停止时使频道获取一个事件

Return a Channel object for one of the current channels. The id must be a value from 0 to the value of pygame.mixer.get_num_channels().
The Channel object can be used to get fine control over the playback of Sounds. A channel can only playback a single Sound at time. Using channels is entirely optional since pygame can manage them by default.
返回当前频道中的一个Channel对象。频道id必须是一个从0到pygame.mixer.get_num_channels()的数值。
Channel对象可用于很好的控制Sound的播放。一个频道一次只能播放一个单一Sound。使用频道完全是可选的因为pygame默认情况下可以管理它们。

play()
play a Sound on a specific Channel
play(Sound, loops=0, maxtime=0, fade_ms=0) -> None
This will begin playback of a Sound on a specific Channel. If the Channel is currently playing any other Sound it will be stopped.
The loops argument has the same meaning as in Sound.play(): it is the number of times to repeat the sound after the first time. If it is 3, the sound will be played 4 times (the first time, then three more). If loops is -1 then the playback will repeat indefinitely.
播放某一特定频道上的一个Sound
play(Sound, loops=0, maxtime=0, fade_ms=0) -> None
将会在一个指定的Channel上开始播放一个Sound。如果该Channel当前正在播放任何其他Sound,那个声音会被终止。
Loops参数与Sound.play()有着相同的含义:它是开始第一次播放之后重复的次数。如果是3,声音会播放4遍(先1遍,然后再3遍)。如果loops为-1则会无限制播放下去。
As in Sound.play(), the maxtime argument can be used to stop playback of the Sound after a given number of milliseconds.
As in Sound.play(), the fade_ms argument can be used fade in the sound.
正如Sound.play(),maxtime参数可用来使该Sound在播放了给定毫秒数后停止。同样,fade_ms参数可用来渐弱声音。

stop()
stop playback on a Channel
停止一个频道上的播放
stop() -> None
Stop sound playback on a channel. After playback is stopped the channel becomes available for new Sounds to play on it.
停止在一个频道上声音的播放。当一个频道的播放被停止后又可被用于新的Sound的播放。

pause()
temporarily stop playback of a channel
暂停一个频道上的播放
pause() -> None
Temporarily stop the playback of sound on a channel. It can be resumed at a later time with Channel.unpause()
暂停一个频道上声音的播放。之后可用Channel.unpause()恢复播放。

unpause()
resume pause playback of a channel
取消暂停一个频道上的播放
unpause() -> None
Resume the playback on a paused channel.
取消暂停一个频道上的播放。

fadeout()
stop playback after fading channel out
频道渐弱后停止播放
fadeout(time) -> None
Stop playback of a channel after fading out the sound over the given time argument in milliseconds.
在声音渐弱time参数指定的毫秒数之后停止频道的播放。

set_volume()
set the volume of a playing channel
设置一个正在播放的频道的音量
set_volume(value) -> None
set_volume(left, right) -> None
Set the volume (loudness) of a playing sound. When a channel starts to play its volume value is reset. This only affects the current sound. The value argument is between 0.0 and 1.0.
设置播放声音的音量(响度)。当一个频道开始播放时其音量值被重置。这仅对当前声音有影响。value参数介于0.0与1.0之间。
If one argument is passed, it will be the volume of both speakers. If two arguments are passed and the mixer is in stereo mode, the first argument will be the volume of the left speaker and the second will be the volume of the right speaker. (If the second argument is None, the first argument will be the volume of both speakers.)
如果仅传递一个参数,将被视为两个喇叭共同的音量。如果传递两个参数且混合器处于立体音模式,则第一个参数为左扬声器的音量、第二个为右扬声器的音量。(如果第二个参数为None,第一个参数则为两个喇叭共同的音量。)
If the channel is playing a Sound on which set_volume() has also been called, both calls are taken into account. For example:
sound = pygame.mixer.Sound("s.wav")
channel = s.play()      # Sound plays at full volume by default
sound.set_volume(0.9)   # Now plays at 90% of full volume.
sound.set_volume(0.6)   # Now plays at 60% (previous value replaced).
channel.set_volume(0.5) # Now plays at 30% (0.6 * 0.5).

get_volume()
get the volume of the playing channel
获取一个正在播放的频道的音量
get_volume() -> value
Return the volume of the channel for the current playing sound. This does not take into account stereo separation used by Channel.set_volume(). The Sound object also has its own volume which is mixed with the channel.
返回当前正在播放的声音所在频道的音量。该函数不考虑Channel.set_volume()所用的立体分离。Sound对象也有自己的音量用于与频道相混。

get_busy()
check if the channel is active
检查该频道是否活跃
get_busy() -> bool
Returns true if the channel is actively mixing sound. If the channel is idle this returns False.
如果该频道当前在混合声音则返回True。如果频道空闲则返回False。

get_sound()
get the currently playing Sound
获取当前正在播放的Sound
get_sound() -> Sound
Return the actual Sound object currently playing on this channel. If the channel is idle None is returned.
返回在该频道上当前正在播放的实际的Sound对象。如果频道空闲则返回None。

queue()
queue a Sound object to follow the current
使一个Sound对象紧接当前排队
queue(Sound) -> None
When a Sound is queued on a Channel, it will begin playing immediately after the current Sound is finished. Each channel can only have a single Sound queued at a time. The queued Sound will only play if the current playback finished automatically. It is cleared on any other call to Channel.stop() or Channel.play().
If there is no sound actively playing on the Channel then the Sound will begin playing immediately.
当一个Sound在一个频道排队时,在当前声音结束后它将立即开始播放。每个频道每次只能有一个单一Sound排队。该排队的Sound只有在当前播放自动完成后才会播放。该Sound在当调用Channel.stop()或者Channel.play()时会被清理。

get_queue()
return any Sound that is queued
返回任何在排队中的Sound
get_queue() -> Sound
If a Sound is already queued on this channel it will be returned. Once the queued sound begins playback it will no longer be on the queue.
如果一个Sound已经在该频道排队则将其返回。一旦该排队的声音开始播放它就不再在队列之中。

set_endevent()
have the channel send an event when playback stops
当播放停止时使频道引发一个事件
set_endevent() -> None
set_endevent(type) -> None
When an endevent is set for a channel, it will send an event to the pygame queue every time a sound finishes playing on that channel (not just the first time). Use pygame.event.get() to retrieve the endevent once it’s sent.
Note that if you called Sound.play(n) or Channel.play(sound,n), the end event is sent only once: after the sound has been played “n+1” times (see the documentation of Sound.play).
If Channel.stop() or Channel.play() is called while the sound was still playing, the event will be posted immediately.
The type argument will be the event id sent to the queue. This can be any valid event type, but a good choice would be a value between pygame.locals.USEREVENT and pygame.locals.NUMEVENTS. If no type argument is given then the Channel will stop sending endevents.
当一个频道被设置了一个endevent时,则每当一个声音在那个频道上完成播放时(不只是第一次)就会向pygame queue发送一个事件。一旦被发送了,可使用pygame.event.get()获取该endevet。
注意如果你调用了Sound.play(n) 或 Channel.play(sound,n),该endevent仅会发送一次:仅在声音已被播放了’n+1’次后(见Sound.play)。
如果当声音仍旧在播放时调用了Channel.stop() 或 Channel.play(),那么该事件会被立即引发。
type参数为被发送到队列的事件的 id,可为任何有效的事件类型,不过最好选择介于pygame.locals.USEREVENT和pygame.locals.NUMEVENTS的数值。如果不给出tpye参数,那么该频道会停止发送endevent。

get_endevent()
get the event a channel sends when playback stops
当播放停止时使频道获取一个事件
get_endevent() -> type
Returns the event type to be sent every time the Channel finishes playback of a Sound. If there is no endevent the function returns pygame.NOEVENT.
返回每当该频道完成一个Sound的播放后将要被发送的事件的类型。如果没有endevent,那么该函数返回pygame.NOEVENT事件。

---- End pygame.mixer -----
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2018-1-11 10:57:13 | 显示全部楼层
hhhhhhhhhhhhhhhhhhh
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-4 22:14:48 | 显示全部楼层
厉害了,向你学习!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-12 13:27:29 | 显示全部楼层
pygame.mixer
用于加载和播放声音的pygame模块

函数

pygame.mixer.init        -        初始化混音器模块
pygame.mixer.pre_init        -        预设混音器初始化参数
pygame.mixer.quit        -        未初始化混音器
pygame.mixer.get_init        -        测试混音器是否初始化
pygame.mixer.stop        -        停止播放所有声道
pygame.mixer.pause        -        暂时停止播放所有声道
pygame.mixer.unpause        -        恢复暂停播放声道
pygame.mixer.fadeout        -        停止前淡出所有声音的音量
pygame.mixer.set_num_channels        -        设置播放频道的总数
pygame.mixer.get_num_channels        -        获取播放频道的总数
pygame.mixer.set_reserved        -        预留频道自动使用
pygame.mixer.find_channel        -        找到一个未使用的频道
pygame.mixer.get_busy        -        测试是否混合了任何声音
pygame.mixer.Sound        -        从文件或缓冲区对象创建新的Sound对象
pygame.mixer.Channel        -        创建一个Channel对象来控制播放
此模块包含用于加载Sound对象和控制回放的类。混音器模块是可选的,取决于SDL_mixer。您的程序应该测试pygame.mixerpygame模块在加载和播放声音之前是否可用并在使用之前初始化。

调音台模块具有有限数量的声道播放声道。通常程序会告诉pygame开始播放音频,它会自动选择一个可用的频道。默认为8个并发通道,但复杂的程序可以更精确地控制通道数量及其使用。

所有声音播放都混合在后台线程中。当您开始播放Sound对象时,它会在声音继续播放时立即返回。单个Sound对象也可以多次主动播放。

调音台还有一个特殊的流媒体通道。这是用于音乐播放,可通过pygame.mixer.musicpygame模块访问,以控制流式音频模块。

混音器模块必须像其他pygame模块一样初始化,但它有一些额外的条件。该pygame.mixer.init()函数采用几个可选参数来控制播放速率和样本大小。Pygame将默认为合理的值,但pygame无法执行声音重采样,因此应初始化混音器以匹配音频资源的值。

注意:不要减少迟钝的声音,使用较小的缓冲区大小。默认设置为减少某些计算机上发出沙哑声音的可能性。您可以通过调用更改默认缓冲pygame.mixer.pre_init()预设混频器初始化参数之前 pygame.mixer.init()初始化混频器模块或pygame.init()初始化所有进口pygame的模块被调用。例如: 在pygame 1.8中,默认大小从1024更改为3072。pygame.mixer.pre_init(44100,-16,2, 1024)

函数详解
pygame.mixer.init()
初始化混音器模块
init(frequency=22050, size=-16, channels=2, buffer=4096) -> None
初始化混音器模块以进行声音加载和播放。可以覆盖默认参数以提供特定的音频混合。关键字参数被接受。对于参数设置为零的向后兼容性,使用默认值(可能由pre_init调用更改)。

size参数表示每个音频样本使用的位数。如果值为负,则将使用带符号的样本值。正值表示将使用未签名的音频样本。无效值会引发异常。

pygame 2中的新功能(使用SDL2编译时) - 大小可以是32(32位浮点数)。

channels参数用于指定是使用单声道还是立体声。1表示单声道,2表示立体声。不支持其他值(负值被视为1,大于2的值被视为2)。

buffer参数控制混音器中使用的内部采样数。默认值应适用于大多数情况。可以降低它以减少延迟,但可能会发生声音丢失。它可以被提升到更大的值,以确保播放永远不会跳过,但它会对声音播放施加延迟。缓冲区大小必须是2的幂(如果不是,则向上舍入到下一个最接近的2的幂)。

某些平台需要pygame.mixerpygame模块来加载和播放声音模块,以便在显示模块初始化后进行初始化。顶级pygame.init() 自动处理此问题,但无法将任何参数传递给混合器init。为了解决这个问题,混频器具有pygame.mixer.pre_init()在使用顶层初始化之前设置正确默认值的功能。

不止一次调用它是安全的,但是在初始化混音器后,如果没有先调用 pygame.mixer.quit(),则无法更改播放参数 。

pygame.mixer.pre_init()
预设混音器初始化参数
pre_init(frequency=22050, size=-16, channels=2, buffersize=4096) -> None
调用pre_init可以更改调用real时使用的默认值 pygame.mixer.init()。关键字参数被接受。设置自定义混音器播放值的最佳方法是 pygame.mixer.pre_init()在调用顶级之前调用pygame.init()。对于向后兼容性参数,零值将替换为启动默认值。

pygame.mixer.quit(
退出混音器
quit() -> None
这将pygame.mixer取消初始化pygame模块以加载和播放声音。如果稍后重新初始化,则所有播放将停止并且任何加载的Sound对象可能与调音台不兼容。

pygame.mixer.get_init()
测试混音器是否初始化
get_init() -> (frequency, format, channels)
如果混合器已初始化,则返回正在使用的回放参数。如果混音器尚未初始化,则返回None

pygame.mixer.stop()
停止播放所有声道
stop() -> None
这将停止所有有效混音器通道的所有播放。

pygame.mixer.pause()
暂时停止播放所有声道
pause() -> None
这将暂时停止有效混音器通道上的所有播放。稍后可以恢复播放pygame.mixer.unpause()

pygame.mixer.unpause()
恢复暂停播放声道
unpause() -> None
这将在暂停后恢复所有活动声道。

pygame.mixer.fadeout()
停止前淡出所有声音的音量
fadeout(time) -> None
这将在时间参数上淡出所有活动通道上的音量,以毫秒为单位。声音静音后,播放将停止。

pygame.mixer.set_num_channels()
设置播放频道的总数
set_num_channels(count) -> None
设置调音台的可用频道数。默认值为8.可以增加或减少该值。如果该值减小,则截断的通道上播放的声音将停止。

pygame.mixer.get_num_channels()
获取播放频道的总数
get_num_channels() -> count
返回当前活动的播放通道数。

pygame.mixer.set_reserved()
预留频道自动使用
set_reserved(count) -> None
调音台可以保留任意数量的通道,这些通道不会被声音自动选择播放。如果声音当前正在预留频道播放,则不会停止。

这允许应用程序为重要声音保留特定数量的声道,这些声音不得被丢弃或具有可保证的频道。

pygame.mixer.find_channel()
找到一个未使用的频道
find_channel(force=False) -> Channel
这将找到并返回一个非活动的Channel对象。如果没有非活动通道,则此功能将返回None。如果没有非活动通道且force参数为True,则会找到运行时间最长的声道并返回它。

如果调音台有保留频道,pygame.mixer.set_reserved()则此处不会返回这些频道。

pygame.mixer.get_busy()
测试mixer 是否正忙
get_busy() -> bool
如果混音器正忙,则返回True。如果混音器处于空闲状态,则返回False。

pygame.mixer.Sound
从文件或缓冲区对象创建新的Sound对象
Sound(filename) -> Sound
Sound(file=filename) -> Sound
Sound(buffer) -> Sound
Sound(buffer=buffer) -> Sound
Sound(object) -> Sound
Sound(file=object) -> Sound
Sound(array=object) -> Sound

pygame.mixer.Sound.play        -        开始播放声音
pygame.mixer.Sound.stop        -        停止声音播放
pygame.mixer.Sound.fadeout        -        淡出后停止声音播放
pygame.mixer.Sound.set_volume        -        设置此声音的播放音量
pygame.mixer.Sound.get_volume        -        获取播放音量
pygame.mixer.Sound.get_num_channels        -        计算此声音播放的次数
pygame.mixer.Sound.get_length        -        得到声音的长度
pygame.mixer.Sound.get_raw        -        返回Sound样本的bytestring副本。
从文件名,python文件对象或可读缓冲区对象加载新的声音缓冲区。将执行有限的重新采样以帮助样本匹配混音器的初始化参数。Unicode字符串只能是文件路径名。Python 2.x字符串或Python 3.x字节对象可以是路径名或缓冲区对象。使用'file'或'buffer'关键字来避免歧义; 否则声音可能会猜错。如果使用了array关键字,则该对象应该导出版本3,C级别数组接口,或者对于Python 2.6或更高版本,导出新的缓冲区接口(首先检查该对象的缓冲区接口。)

Sound对象表示实际的声音样本数据。更改Sound对象状态的方法将是Sound播放的所有实例。Sound对象还导出数组接口,对于Python 2.6或更高版本,还会导出新的缓冲区接口。

可以从OGG音频文件或未压缩的文件加载声音 WAV。

注意:缓冲区将在内部复制,不会在它与Sound对象之间共享数据。

目前缓冲区和数组支持与sndarray.make_sound 数值数组一致,因为忽略了样本符号和字节顺序。这将通过正确处理符号和字节顺序或在不同时引发异常来改变。此外,截断源样本以适合音频样本大小。这不会改变。

pygame.mixer.Sound(buffer)pygame中pygame.mixer.Sound的新功能 1.8 从 pygame 1.9.2中的文件或缓冲区对象关键字参数和数组接口支持创建新的Sound对象

play()
开始播放声音
play(loops=0, maxtime=0, fade_ms=0) -> Channel
在可用频道上开始播放声音(即,在计算机的扬声器上)。这将强制选择一个频道,因此如有必要,播放可能会切断当前正在播放的声音。

loops参数控制第一次播放后样本重复的次数。值5表示声音将播放一次,然后重复播放五次,因此共播放六次。默认值(零)表示声音不重复,因此只播放一次。如果循环设置为-1,Sound将无限循环(尽管您仍然可以调用stop()它来停止它)。

maxtime参数可用于在给定的毫秒数后停止播放。

fade_ms参数将使声音以0音量开始播放,并在给定时间内逐渐淡出至全音量。样品可以在淡入完成之前结束。

这将返回所选通道的Channel对象。

stop()
停止声音播放
stop() -> None
这将停止在任何活动频道上播放此声音。

fadeout()
淡出后停止声音播放
fadeout(time) -> None
这将在以毫秒为单位在时间参数上淡出后停止播放声音。声音会在所有主动播放的频道上消失并停止。

set_volume()
设置此声音的播放音量
set_volume(value) -> None
这将设置此声音的播放音量(响度)。如果正在播放,这将立即影响声音。它也会影响此声音的任何未来播放。参数是从0.0到1.0的值。

get_volume()
获取播放音量
get_volume() -> value
返回0.0到1.0之间的值,表示此Sound的音量。

get_num_channels()
计算此声音播放的次数
get_num_channels() -> count
返回此声音正在播放的活动频道数。

get_length()
得到声音的长度
get_length() -> seconds
以秒为单位返回此声音的长度。

get_raw()
返回Sound样本的bytestring副本。
get_raw() -> bytes
将Sound对象缓冲区的副本作为字节(对于Python 3.x)或str(对于Python 2.x)对象返回。

pygame 1.9.2中的新功能。

pygame.mixer.Channel
创建一个Channel对象来控制播放
Channel(id) -> Channel

pygame.mixer.Channel.play        -        在特定频道播放声音
pygame.mixer.Channel.stop        -        停止在频道上播放
pygame.mixer.Channel.pause        -        暂时停止播放频道
pygame.mixer.Channel.unpause        -        恢复暂停播放频道
pygame.mixer.Channel.fadeout        -        淡出通道后停止播放
pygame.mixer.Channel.set_volume        -        设置播放频道的音量
pygame.mixer.Channel.get_volume        -        获得播放频道的音量
pygame.mixer.Channel.get_busy        -        检查通道是否处于活动状态
pygame.mixer.Channel.get_sound        -        得到当前播放的声音
pygame.mixer.Channel.queue        -        排队Sound对象以跟随当前
pygame.mixer.Channel.get_queue        -        返回排队的任何声音
pygame.mixer.Channel.set_endevent        -        播放停止时让频道发送事件
pygame.mixer.Channel.get_endevent        -        获取播放停止时频道发送的事件
返回其中一个当前通道的Channel对象。id必须是从0到值的值pygame.mixer.get_num_channels()。

Channel对象可用于精确控制Sounds的播放。一个频道只能播放一个声音。使用频道完全是可选的,因为pygame默认可以管理它们。

play()
在特定频道播放声音
play(Sound, loops=0, maxtime=0, fade_ms=0) -> None
这将开始播放特定频道上的声音。如果频道正在播放任何其他声音,它将被停止。

loops参数具有与以下相同的含义Sound.play():它是第一次重复声音的次数。如果是3,声音将播放4次(第一次,然后是三次)。如果循环为-1,则播放将无限重复。

同样Sound.play(),maxtime参数可用于在给定的毫秒数后停止播放声音。

在中Sound.play(),fade_ms参数可用于淡入声音。

stop()
停止在频道上播放
stop() -> None
停止在频道上播放声音。播放停止后,频道可用于播放新的声音。

pause()
暂时停止播放频道
pause() -> None
暂时停止在频道上播放声音。它可以在以后恢复Channel.unpause()

unpause()
恢复暂停播放频道
unpause() -> None
在暂停的频道上恢复播放。

fadeout()
淡出通道后停止播放
fadeout(time) -> None
在给定时间参数上淡出声音后,以毫秒为单位停止播放通道。

set_volume()
设置播放频道的音量
set_volume(value) -> None
set_volume(left, right) -> None
设定播放声音的音量(响度)。当频道开始播放时,其音量值将被重置。这只会影响当前的声音。value参数介于0.0和1.0之间。

如果传递一个参数,则它将是两个发言者的音量。如果传递两个参数并且混音器处于立体声模式,则第一个参数将是左扬声器的音量,第二个参数将是右扬声器的音量。(如果第二个参数为None,则第一个参数将是两个扬声器的音量。)

如果频道正在播放set_volume()已调用的声音,则会同时考虑这两个呼叫。例如:

sound = pygame.mixer.Sound("s.wav")
channel = s.play()      # Sound plays at full volume by default
sound.set_volume(0.9)   # Now plays at 90% of full volume.
sound.set_volume(0.6)   # Now plays at 60% (previous value replaced).
channel.set_volume(0.5) # Now plays at 30% (0.6 * 0.5).

get_volume()
获得播放频道的音量
get_volume() -> value
返回当前播放声音的通道音量。这没有考虑到使用的立体声分离 Channel.set_volume()。Sound对象也有自己的音量,与音频混合。

get_busy()
检查通道是否处于活动状态
get_busy() -> bool
如果通道正在主动混合声音,则返回true。如果通道空闲,则返回False。

get_sound()
得到当前播放的声音
get_sound() -> Sound
返回当前在此频道上播放的实际Sound对象。如果通道空闲,则返回None。

queue()
排队Sound对象以跟随当前
queue(Sound) -> None
当声音在频道上排队时,它将在当前声音结束后立即开始播放。每个通道一次只能排队一个声音。排队的声音仅在当前播放自动结束时播放。在对Channel.stop()或的任何其他呼叫中清除它 Channel.play()。

如果在频道上没有主动播放声音,则声音将立即开始播放。

get_queue()
返回排队的任何声音
get_queue() -> Sound
如果声音已在此频道上排队,则会返回该声音。一旦排队的声音开始播放,它将不再在队列中。

set_endevent()
播放停止时让频道发送事件
set_endevent() -> None
set_endevent(type) -> None
当为某个频道设置了一个尝试时,每当一个声音在该频道上播放时(不仅仅是第一次),它就会向一个游戏队列发送一个事件。使用pygame.event.get()一旦它发送到检索ENDEVENT。

请注意,如果您调用Sound.play(n)或Channel.play(sound,n),结束事件仅发送一次:声音播放“n + 1”次后(请参阅Sound.play文档)。

如果在声音仍然播放时调用Channel.stop()或Channel.play()调用,则会立即发布事件。

type参数将是发送到队列的事件id。这可以是任何有效的事件类型,但一个好的选择是pygame.locals.USEREVENT和之间的值 pygame.locals.NUMEVENTS。如果没有给出类型参数,那么Channel将停止发送事件。

get_endevent()
获取播放停止时频道发送的事件
get_endevent() -> type
返回每次Channel完成声音播放时要发送的事件类型。如果没有功能返回该功能 pygame.NOEVENT。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-2 21:29:19 | 显示全部楼层
梦想绘制者 发表于 2017-12-19 23:46
看到还没有翻译,于是自己动手翻译了一下以飨大家,希望能有点用途。不过由于我水平和知识面有限,错误和纰 ...

666666膜拜大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-18 12:41:35 | 显示全部楼层
没有music模块呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-27 05:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表