このmoduleには、音声オブジェクトを読み込んだり再生操作を行うためにクラスが含まれています。このmixerモジュールは単体で機能するものではなく、SDL_mixerに依存しています。プログラムでpygame.mixerモジュールを使用する場合は、あらかじめ使用可能かや初期化できているかを確認したほうがよいでしょう。
この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.一つのSuoudオブジェクトで何度でも音声を再生することができます。
また、mixerモジュールはもう一つ特殊なストリーミング用のチャンネルを持っています。これは音楽を再生するため使うためのもので、 pygame.mixer.musicモジュールを通じて操作できます。
このmixerモジュールは他のpygameモジュールと同様あらかじめ初期化しておかなければなりませんが、他に追加で必要な操作がいくつかあります。pygame.mixer.init命令では再生レートや、オーディオサンプルサイズを制御するためにいくつかの引数を追加で設定できます。Pygameが適切な値を既定で設定してくれますが、pygameは音声の聞き比べまではできないので、あなたのオーディオ環境に合わせた適切な値で初期化をしたほうがよいでしょう。
メモ:音ズレをなくすためには、バッファサイズを小さく設定してください。既定では、一部の環境での雑音が少なくなるようバッファサイズが設定されています。pygame.mixer.init命令やpygame.init命令を実行する前に、pygame.mixer.pre_init命令を実行することで既定のバッファサイズを変更することができます。 例: pygame.mixer.pre_init(44100,-16,2, 1024)。pygameのバージョン1.8より、バッファの既定サイズは1024から3072へ変更されました。
※現在のバッファ既定サイズは4096です。
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.
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.
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.
The mixer also has a special streaming channel. This is for music playback and is accessed through the pygame.mixer.music module.
The mixer module must be initialized like other pygame modules, but it has some extra conditions. The pygame.mixer.init - initialize the mixer module 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.
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 before pygame.mixer.init or pygame.init 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.
音声の読み込みと再生を行うmixerモジュールを初期化します。オーディオ環境に適した任意の値を引数に設定することで、既定の設定値を変更することができます。キーワード引数も使用することができます。後方互換性を保つため、引数に0が設定された場合は既定値に置き換えられて設定されます(pre_init命令を実行することで既定値の変更は可能です)。
size引数は、オーディオサンプルサイズにどれだけのビットが使用されるかを表します。負の値が設定された場合は符号付整数が使用されます。正の値が設定された場合は符号無し整数が使用されます。使用できない値が設定された場合は例外が発生します。
channels引数ではモノラルとステレオのどちらを使用するかを指定します。1が設定され場合はモノラルとなり、2が設定された場合はステレオとなります。他の値は設定することができません。 (1より小さいが設定された場合それは1として扱われ、2より大きい値が設定された場合それは2として扱われます)。
buffer引数を設定してサウンドミキサーで使用されるサンプルサイズを制御します。大抵の環境で問題なく動作するような値が既定値に設定されています。buffer引数に小さい値を設定することで音の遅延時間音を減らすことができますが、それによって音飛びが発生する場合もあります。逆に大きい値を設定することで音飛びは発生しないようにできますが、再生時に遅延が発生してしまいます。buffer引数に設定する値は「1、2、4、8、16・・・」といった2の累乗でなければいけません。 (それ外の値を設定すると、自動的に最も近い2の累乗に切り上げられます)。
使用環境によっては、displayモジュールを初期化した後にpygame.mixerモジュールを初期化しなければいけない場合もあります。最上位にあるpygame.init命令でそれらの初期化は自動的に実行されますが、その際は初期化の引数を設定することができません。この問題を解決するため、pygame.init命令内でmixerモジュール初期化が行われる前に、mixerモジュールの初期化用引数をあらかじめ設定できるpygame.mixer.pre_init命令があります。
この命令は複数回実行されても問題ありませんが、一度初期化が行われるとpygame.mixer.quit命令で解除を行わない限り初期化用引数を再設定することはできません。
※原文では「負の値が設定された場合は1として扱う(negative values are treated as 1)」と記載されていますが、0を設定した場合も1として扱われるようなので「1より小さいが設定された場合それは1として扱う」と訳しています。
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).
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).
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).
Some platforms require the pygame.mixer module to be initialized after the display modules have initialized. The top level pygame.init - initialize all imported pygame modules 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 - preset the mixer init arguments 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 - uninitialize the mixer.
pre_init命令を使用すると、pygame.mixer.init実行時に設定する引数の既定値を変更することができます。この命令ではキーワード引数を使うことができます。最上位初期化命令のpygame.initを実行する前にはこのpygame.mixer.pre_init命令を実行し、mixerモジュールの再生値を変更するのがよいでしょう。後方互換性を保つため、引数に0が設定された場合は既定値に置き換えられて設定されます。
Call pre_init to change the defaults used when the real pygame.mixer.init - initialize the mixer module is called. Keyword arguments are accepted. The best way to set custom mixer playback values is to call pygame.mixer.pre_init - preset the mixer init arguments before calling the top level pygame.init - initialize all imported pygame modules. For backward compatibility argument values of zero is replaced with the startup defaults.
この命令を実行するとpygame.mixerモジュールの初期化が解除されます。全ての再生中の音声は終了されたうえ、再度初期化を実行したとしても作成済みのSoundオブジェクトは使えなくなる可能性があります。
This will uninitialize pygame.mixer. All playback will stop and any loaded Sound objects may not be compatible with the mixer if it is reinitialized later.
mixerモジュールが初期化されている場合は、設定されている再生値が戻り値として返されます。初期化されていない場合はNone値が戻り値として返されます。
If the mixer is initialized, this returns the playback arguments it is using. If the mixer has not been initialized this returns None
この命令を実行すると、スピーカーで現在再生されている全ての音声が一時停止されます。一時停止した後は、pygame.mixer.unpause命令を実行すると再生を再開することができます。
This will temporarily stop all playback on the active mixer channels. The playback can later be resumed with pygame.mixer.unpause - resume paused playback of sound channels
一時停止させた全てのスピーカーでの音声再生を再開させます。
This will resume all active sound channels after they have been paused.
time引数で設定した時間(ミリ秒単位)の間、再生中の全ての音声を徐々に小さくしていきます。音声が聞こえなくなった後に再生が終了されます。
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.
mixerモジュールが使用可能なスピーカーの数を設定します。既定では8が設定されています。設定されている値から小さくすることも大きくすることもできます。値が減らされた場合は、減らされたスピーカーで再生されていた音声は終了します。
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.
現在音声再生に使用可能なスピーカーの数を戻り値として返します。
※原文には「pygame.mixer.get_num_channels(): return count」の記載がないため追記しています。
Returns the number of currently active playback channels.
mixerモジュールが使用するための音声再生用スピーカーを確保しておくことができます。あくまで予約なので、ただちに音声再生に割り込むわけではありません。確保したスピーカーが音声再生中だったとしてもその再生が終了されるようなことはありません。
この命令を使用することで、アプリケーション内で途中で途切れてしまうと困るような重要な音声があった場合はそれ用のスピーカーを予約して音声の再生を保証しておくことができるのです。
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.
この命令では使用されていないスピーカーを探し、Channelオブジェクトの戻り値として返します。未使用のスピーカーがない場合はNone値が戻り値として返されます。force引数にTrueが設定されていた状態で未使用のスピーカーがない場合は、最も長い時間生成されてるスピーカーを探して戻り値として返します。
pygame.mixer.set_reserved命令でスピーカーを確保していた場合は、それら確保済みのスピーカーはここでの取得対象にはなりません。
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 - reserve channels from being automatically used then those channels will not be returned here.
mixerモジュールがミキシング処理を行っている場合はTrueが戻り値として返ります。行っていない場合はFalseが戻り値として返されます。
Returns True if the mixer is busy mixing any channels. If the mixer is idle then this return False.
指定したファイルや読み込み可能なデータオブジェクト、pythonオブジェクトからSoundデータを新規に読み込みます。Limited resampling will be performed to help the sample match the initialize arguments for the mixer.
作成されたSoundオブジェクトは、実際に再生される音声のサンプリングデータを表します。 Methods that change the state of the Sound object will the all instances of the Sound playback.
音声データはOGGファイルや非圧縮形式のWAVファイルを読み込むことができます。
メモ:bufferデータはプログラム内でのみコピーすることができ、Soundオブジェクトとの互換性はありません。
pygame.mixer.Sound(buffer)はpygameのバージョン1.8から新しく実装されました。
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.
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.
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.
pygame.mixer.Sound(buffer) is new in pygame 1.8
使用可能なチャンネル(すなわちコンピューターのスピーカーのことです)を通して音声の再生を開始します。チャンネルを通さなければ音声の再生はできないので、この命令では必要に応じて再生を止めることができます。
loops引数を設定することで、初回の再生後に音声を何回再生するか制御することができます。5を設定した場合は1回目の再生後に5回再生が繰り返されるので、合計6回の再生が行われます。既定では0が設定されており、これは初回再生後に繰り返し再生を行わないということなので、1回のみ音声の再生が行われます。loops引数に-1が設定されていた場合は、音声は無限に再生を繰り返します。(それでもstop()命令を使用することで再生を終了できます)。
maxtime引数を使用して、音声の再生時間を設定することができます。
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.
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.
この命令で音声の再生を終了させます。
This will stop the playback of this Sound on any active Channels.
time引数で設定した時間(ミリ秒単位)の間、再生中の音声を徐々に小さくしてしていった上で終了させます。再生中の全てのチャンネルで、音量が小さくなって終了がされます。
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.
この命令で音声を再生する音量を設定します。音声が再生中の場合、設定した値は即座に反映されます。設定後に再生する全ての音声にこの音量が適用されます。引数には0.0から1.0の範囲内の値を設定します。
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オブジェクトで再生する音量を0.0から1.0の範囲内の値で戻り値として返します。
Return a value from 0.0 to 1.0 representing the volume for this Sound.
この音声が再生されているスピーカーの数を戻り値として返します。
Return the number of active channels this sound is playing on.
音声の再生時間(秒単位)を戻り値として返します。
Return the length of this Sound in seconds.
音声のサンプリングデータのバッファオブジェクトを戻り値として返します。このバッファデータを使用することで、音声データに直接アクセスして変更処理を行うことができます。
この命令はpygameのバージョン1.8で新たに実装されました。
Return a buffer object for the Sound samples. The buffer can be used for direct access and manipulation.
New in pygame 1.8.
現在の音声出力状態を表すChannelオブジェクトを戻り値として返します。ここで設定するid引数は「0~pygame.mixer.get_num_channelsの戻り値」の範囲内出なければなりません。
Channelオブジェクトを使用することで音声の再生を細かく制御することができます。一つChannelオブジェクトでは一度に一つの音声しか再生できません。pygameは元から音声出力の制御を行うことができるので、このオブジェクトは使用してもしなくてもどちらでも構いません。
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 - get the total number of playback 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.
この命令を使用して特定のスピーカーで音声を再生を開始することができます。そのスピーカーで既に他の音声再生が行われていた場合は、その音声は終了されます。
loops引数はSound.play()で使用されているものと同じ、繰り返し再生の回数を設定します。:最初の再生が行われた後に、何回再生が行われるかを表します。loops引数に3が設定された場合、音声の再生は4回行われます(最初に1回再生が行われ、その後3回の再生が繰り返されます)。loops引数に-1が設定されると音声は無限に再生を繰り返します。
Sound.playと同じく、maxtime引数を使用することで音声の再生時間をミリ秒単位で設定できます。
Sound.playと同じく、fade_ms引数を使用することで音声が徐々に小さくなって消えていく時間を設定できます。
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.
>As in Sound.play - begin sound playback, the maxtime argument can be used to stop playback of the Sound after a given number of milliseconds.
As in Sound.play - begin sound playback, the fade_ms argument can be used fade in the sound.
スピーカーで再生されている音声を終了します。再生が終了されると、スピーカーでは別の音声を再生できるようになります。
Stop sound playback on a channel. After playback is stopped the channel becomes available for new Sounds to play on it.
スピーカーで再生されている音声を一時停止します。一時停止した後は、Channel.unpause命令を実行すると再生を再開することができます。
Temporarily stop the playback of sound on a channel. It can be resumed at a later time with Channel.unpause - resume pause playback of a channel
一時停止されているスピーカーでの音声再生を再開します。
Resume the playback on a paused channel.
time引数で設定した時間(ミリ秒単位)の間スピーカー上で再生中の音声を徐々に小さくしていき、終了させます。
Stop playback of a channel after fading out the sound over the given time argument in milliseconds.
この命令で音声を再生する音量を設定します。スピーカーで新たに音声の再生が開始されると、設定した音量はリセットされます。設定した音量は現在再生中の音声にのみに適用されます。value引数には0.0から1.0までの範囲の値を設定します。
引数が一つのみ設定された場合は、その値は左右両方のスピーカーの音量として設定されます。mixerモジュールがステレオモードの状態で引数が二つ設定された場合、最初の引数は左のスピーカーの音量となり、二番目の引数は右のスピーカーの音量となります。(二番目の引数にNone値が設定された場合、最初の引数が左右両方のスピーカーの音量として設定されます。)
再生中の音声で複数回set_volume()命令が実行された場合、それぞれの命令はちゃんと適用されます。例えば:
sound = pygame.mixer.Sound("s.wav")
channel = sound.play() # 既定では最大ボリュームで音声が再生されます。
sound.set_volume(0.9) # ここからは最大ボリュームの90%で音声が再生されます。
sound.set_volume(0.6) # ここからは最大ボリュームの60%で音声が再生されます (先ほど設定した値は書き換えられます)。
channel.set_volume(0.5) # ここからは最大ボリュームの30%で音声が再生されます (0.6 * 0.5)。
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.
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.)
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).
現在音声を再生しているスピーカーの音量を戻り値として返します。Channel.set_volume命令で左右別々の音量が設定されていたとしても問題ありません。Soundオブジェクトもそれぞれのスピーカーの音量を組み合わせて算出した独自の音量を保持しています。
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.
スピーカーが音声再生中の場合はTrueが戻り値として返されます。スピーカーが待機状態の場合はFalseが戻り値として返されます。
Returns true if the channel is activily mixing sound. If the channel is idle this returns False.
このスピーカーで現在再生している音声をSoundオブジェクト型の戻り値として返します。スピーカーが待機状態の場合はNone値が戻り値として返されます。
Return the actual Sound object currently playing on this channel. If the channel is idle None is returned.
音声がスピーカーの再生順番待ちに設定されると、現在再生中の音声が終了すると即座に順番待ち音声の再生が行われます。各Channelには一度に一つの順番待ち音声しか並ぶことができません。順番待ち音声は、現在再生中の音声が正常に終了した場合にのみ再生が行われます。Channel.stop命令やChannel.play命令によってい正常な終了が妨げられると順番待ち音声は消去されます。
Channelで再生が行われていない場合は、即座に音声の再生が開始されます。
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 - stop playback on a Channel or Channel.play - play a Sound on a specific Channel.
If there is no sound actively playing on the Channel then the Sound will begin playing immediately.
Channelに再生順番待ち音声が並んでいた場合、それを戻り値として返します。既に再生中の音声を順番待ちに設定することはできません。
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.
channelに終了イベントが設定されると、channelでの音声再生が終了する度にそのイベントをpygameへ送信します(初回再生時だけイベントが発生するわけではありません)。pygame.event.get命令を使用すると設定された終了イベントを取得することができます。
Sound.play(n)命令やChannel.play(sound,n)命令で複数回再生を実行れた場合でも、終了イベントは一度しか発生しないので注意してください。:音声が"n+1"回された後にイベントが発生します(詳しくはSound.playの項目を参照してください)。
音声再生中にChannel.stop命令やChannel.play命令が実行されると、即座に終了イベントが発生します。
type引数にはpygameへ送信するイベントのIDを設定します。使用可能なイベントタイプであれば何でも設定できますが、pygame.locals.USEREVENT定数(24)とpygame.locals.NUMEVENTS定数(32)の間、25以上31以下の値を設定するのがよいでしょう。type引数を設定しないで命令を実行した場合、終了イベントの送信は解除されます。
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 - get events from the queue 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 - stop playback on a Channel or Channel.play - play a Sound on a specific Channel 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.
Channelでの音声再生が終了する度に発生するイベントタイプを戻り値として取得します。終了イベントが設定されてない場合は、pygame.NOEVENT定数(0)が戻り値として取得されます。
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.