サーチ…


Pygletでオーディオ

import pyglet
audio = pyglet.media.load("audio.wav")
audio.play()

詳細は、「 pyglet 」を参照してください。

WAVファイルの操作

winsound

  • Windows環境
import winsound
winsound.PlaySound("path_to_wav_file.wav", winsound.SND_FILENAME)

  • モノラル/ステレオをサポート
  • 圧縮/解凍をサポートしていません
import wave
with wave.open("path_to_wav_file.wav", "rb") as wav_file:    # Open WAV file in read-only mode.
    # Get basic information.
    n_channels = wav_file.getnchannels()      # Number of channels. (1=Mono, 2=Stereo).
    sample_width = wav_file.getsampwidth()    # Sample width in bytes.
    framerate = wav_file.getframerate()       # Frame rate.
    n_frames = wav_file.getnframes()          # Number of frames.
    comp_type = wav_file.getcomptype()        # Compression type (only supports "NONE").
    comp_name = wav_file.getcompname()        # Compression name.

    # Read audio data.
    frames = wav_file.readframes(n_frames)    # Read n_frames new frames.
    assert len(frames) == sample_width * n_frames

# Duplicate to a new WAV file.
with wave.open("path_to_new_wav_file.wav", "wb") as wav_file:    # Open WAV file in write-only mode.
    # Write audio data.
    params = (n_channels, sample_width, framerate, n_frames, comp_type, comp_name)
    wav_file.setparams(params)
    wav_file.writeframes(frames)

任意のサウンドファイルをpythonとffmpegで変換する

from subprocess import check_call

ok = check_call(['ffmpeg','-i','input.mp3','output.wav'])
if ok:
    with open('output.wav', 'rb') as f:
        wav_file = f.read()

注意:

Windowsのビープ音を鳴らす

Windowsでは明示的なインターフェイスが提供されているため、 winsoundモジュールを使用して、指定した周波数と期間で元のビープ音を再生することができます。

import winsound
freq = 2500 # Set frequency To 2500 Hertz
dur = 1000 # Set duration To 1000 ms == 1 second
winsound.Beep(freq, dur)


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow