このmusicモジュールはpygame.mixerと密接につながっています。musicモジュールを使用することでサウンドミキサーでの音楽の再生を制御できます。
音楽再生と通常の音声再生の違いについてですが、音楽はストリーミング再生なので再生前に一度にファイル全てを読み込むことはありません。 このミキサーでは一度に一つの音楽しか再生することができません。
MP3ファイルのサポートには制限があるので気をつけてください。使用環境によっては、サポートされていないファイル形式を使用するとプログラムがクラッシュする可能性もあります(例えば、Debian Linuxなど)。MP3ファイルがサポートされていない環境では、代わりにOGGファイルの使用を検討してください。
The music module is closely tied to pygame.mixer. Use the music module to control the playback of music in the sound mixer.
The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.
Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.
この命令で音楽ファイル名や音楽オブジェクトを読み込み、再生の準備をします。既に再生中の音楽があった場合、その再生は停止されます。この命令だけでは音楽の再生は始まりません。
This will load a music filename/file object and prepare it for playback. If a music stream is already playing it will be stopped. This does not start the music playing.
読み込んだ音楽をストリーミング再生します。既に再生中の音楽だった場合は、もう一度最初から再生が始まります。
loops引数に値を設定することで、音楽の繰り返し再生回数を制御します。play(5)で実行すると、一度再生を行った後に5回の繰り返し再生が行われるので合計6回の再生されます。loops引数に-1が設定されていた場合、音楽再生は無限に繰り返されます。
start引数を設定することで、音楽の再生開始位置を制御することができます。再生開始位置は、再生する音楽ファイルの形式によって左右されます。MP3ファイルやOGGファイルの場合、start引数に設定した開始位置はミリセカンド単位の時間として扱われます。MODファイルの場合だと再生するパターンの番号として扱われます(MODファイルではパターンと呼ばれる演奏データを順序良く並べて音楽を再生する)。設定できない値をstart引数に設定した場合、エラー:NotImplementedErrorが発生します。
This will play the loaded music stream. If the music is already playing it will be restarted.
The loops argument controls the number of repeats a music will play. play(5) will cause the music to played once, then repeated five times, for a total of six. If the loops is -1 then the music will repeat indefinitely.
The starting position argument controls where in the music the song starts playing. The starting position is dependent on the format of music playing. MP3 and OGG use the position as time (in seconds). MOD music it is the pattern order number. Passing a startpos will raise a NotImplementedError if it cannot set the start position
現在再生されている音楽をリセットして、もう一度最初から再生を開始します。
Resets playback of the current music to the beginning.
音楽が再生中の場合、それを終了させます。
Stops the music playback if it is currently playing.
再生中の音楽を一時停止します。pygame.mixer.music.unpause命令を実行することで一時停止した音楽を再開できます。
Temporarily stop playback of the music stream. It can be resumed with the pygame.mixer.music.unpause - resume paused music function.
一時停止させた音楽の再生を再開させます。
This will resume the playback of a music stream after it has been paused.
time引数で設定した時間(ミリ秒単位)の中で再生中の音楽の音を徐々に小さくしていった上で、再生を終了します。
※原文には「再生が終了するまでの間、この命令はプログラムのブロックを行います(Note, that this function blocks until the music has faded out.)」と記載されていますが、フェードアウト中でも音楽の停止や再生が行えるようなので、その部分の記載は省いています。
This will stop the music playback after it has been faded out over the specified time (measured in milliseconds).
Note, that this function blocks until the music has faded out.
再生する音楽のボリュームを設定します。value引数には0.0から1.0までの値を設定します。現在再生中のものとは別の音楽が新たに読み込まれた場合、設定したボリュームの値はリセットされます。
Set the volume of the music playback. The value argument is between 0.0 and 1.0. When new music is loaded the volume is reset.
ミキサーの現在のボリュームを戻り値として取得します。取得されるボリュームは0.0から1.0までの値となります。
Returns the current volume for the mixer. The value will be between 0.0 and 1.0.
音楽が再生中の場合はTrueが戻り値として返されます。再生されていない場合はFalseが戻り値として返されます。
Returns True when the music stream is actively playing. When the music is idle this returns False.
音楽が再生された時間をミリセカンド単位の値で取得します。ここで取得される時間は、単純に再生された時間のみを表します。;再生開始位置によって時間が加算されるようなことはありません。
This gets the number of milliseconds that the music has been playing for. The returned time only represents how long the music has been playing; it does not take into account any starting position offsets.
この命令では音楽ファイルを読み込み、再生の順番待ちに設定します。順番待ちの音楽は、現在再生中の音楽が正常に終了した後に再生が開始されます。再生中の音楽が途中で終了したり変更されたりした場合、順番待ち音楽の再生は行われません。
下記の例ではbach.oggを6回再生した後にmozart.oggを1回再生します:
pygame.mixer.music.load('bach.ogg')
pygame.mixer.music.play(5) #5回ではなく6回再生が行われます!
pygame.mixer.music.queue('mozart.ogg')
This will load a music file and queue it. A queued music file will begin as soon as the current music naturally ends. If the current music is ever stopped or changed, the queued song will be lost.
The following example will play music by Bach six times, then play music by Mozart once:
pygame.mixer.music.load('bach.ogg')
pygame.mixer.music.play(5) # Plays six times, not five!
pygame.mixer.music.queue('mozart.ogg')
この命令によって、音楽の再生が終了した時にPygameがイベントキューの信号を発生させます。設定する引数によってキューに送られるイベントのタイプを決定します。
設定されたイベントは一度限りのものではなく、音楽の再生が終了するたびに毎回発生します。イベントの発生を解除するためには、引数を設定せずにこの命令を実行します。
This causes Pygame to signal (by means of the event queue) when the music is done playing. The argument determines the type of event that will be queued.
The event will be queued every time the music finishes, not just the first time. To stop the event from being queued, call this method with no argument.
音楽の再生が終了した時に発生するイベントの種類を戻り値として返します。終了イベントが設定されていない場合、pygame.NOEVENTの値が戻り値として返されます。
Returns the event type to be sent every time the music finishes playback. If there is no endevent the function returns pygame.NOEVENT.