Pythonで音楽を演奏する

Pythonで音楽を演奏します.

# ライブラリのインストール

下記のコードでライブラリをインストールします.

# !apt-get install -y pulseaudio
# !apt-get update
# !apt-get install libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y
!pip install pyaudio

# Pythonで音を鳴らしてみよう

音声を出力するための処理をプログラム内で行います。

音は波形になっているため、音階をHz(ヘルツ)というもので表します。

ここでは、サイン波で整形して、音を出力・再生します。

# 既存の音楽を入出力

from pydub import AudioSegment

#ファイル読み込み
sound = AudioSegment.from_file("./data/music/ウォーターランド.m4a", "m4a")

#配列データ取り出し
samples = np.array(sound.get_array_of_samples())

#読み込んだ配列データから新しいAudioSegmentを作成
test = AudioSegment(
                    samples.astype("int16").tobytes(), 
                    sample_width=sound.sample_width, 
                    frame_rate=sound.frame_rate, 
                    channels=sound.channels,
                    )

#保存
test.export("./data/music/test.mp3", format="mp3")

# テキトーな音を出力

from pydub import AudioSegment
import numpy as np

#配列データ取り出し
samples=np.sin(np.arange(50000)/20)

#サイン波を-32768から32767の整数値に変換(signed 16bit pcmへ)
stream = np.array([int(x * 32767.0) for x in samples])

#読み込んだ配列データから新しいAudioSegmentを作成
test = AudioSegment(
                    stream.astype("int16").tobytes(), 
                    sample_width=2, 
                    frame_rate=44100, 
                    channels=1,
                    )
#保存
test.export("./data/music/sample.mp3", format="mp3")

# YOASOBI「夜に駆ける」を演奏する

下記のサイトで音階を周波数で変換する。

https://tomari.org/main/java/oto.html

import pyaudio
import numpy as np

#サンプリングレートを定義
RATE=44100

#BPMや音長を定義
BPM=120
L1=(60/BPM*4)
L2,L4,L8=(L1/2,L1/4,L1/8)

#ドレミ...の周波数を定義
C,C_s,D,D_s,E,F,F_s,G,G_s,A,A_s,B,C2,D2,D2_s=(
    261.626,277.183,293.665,311.127,329.628,
    349.228,369.994,391.995,415.305,440.000,466.164,
    493.883,523.251,587.330,622.254
)

#サイン波を生成
def tone(freq,length,gain):
    slen=int(length*RATE)
    t=float(freq)*np.pi*2/RATE
    return np.sin(np.arange(slen)*t)*gain

#配列データ取り出し
stream = tone(G,L8,1.0)
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(C2,L4,1.0))
stream = np.append(stream, tone(G_s,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D_s,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(C2,L8,1.0))
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(C2,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D_s,L4,1.0))
stream = np.append(stream, tone(C,L8,1.0))
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(G_s,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D_s,L8,1.0))
stream = np.append(stream, tone(D,L8,1.0))
stream = np.append(stream, tone(D_s,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(G_s,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(C2,L8,1.0))
stream = np.append(stream, tone(A_s,L4,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(C2,L4,1.0))
stream = np.append(stream, tone(G_s,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D2,L8,1.0))
stream = np.append(stream, tone(C2,L8,1.0))
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(A_s,L8,1.0))
stream = np.append(stream, tone(C2,L8,1.0))
stream = np.append(stream, tone(D2,L8,1.0))
stream = np.append(stream, tone(D2_s,L4,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D_s,L4,1.0))
stream = np.append(stream, tone(C,L8,1.0))
stream = np.append(stream, tone(D_s,L8,1.0))
stream = np.append(stream, tone(G_s,L8,1.0))
stream = np.append(stream, tone(G,L8,1.0))
stream = np.append(stream, tone(D_s,L8,1.0))
stream = np.append(stream, tone(F,L8,1.0))
stream = np.append(stream, tone(D_s,L4,1.0))

#サイン波を-32768から32767の整数値に変換(signed 16bit pcmへ)
stream = np.array([int(x * 32767.0) for x in stream])

#読み込んだ配列データから新しいAudioSegmentを作成
test = AudioSegment(
                    stream.astype("int16").tobytes(), 
                    sample_width=2, 
                    frame_rate=44100, 
                    channels=1,
                    )
#保存
test.export("./data/music/yoru_ni_kakeru.mp3", format="mp3")

# まとめ

Pythonで音楽を演奏しました.

# 参考サイト

Pythonで音楽を演奏するレシピ (opens new window)

jiaaro/pydub (opens new window)

NumpyのarrayからPydubのAudioSegmentを作成する (opens new window)

【Audio入門】Sin波の合成でメロディや音声を再現・再生してみる♬ (opens new window)

PyAudioの基本メモ2 音声入出力 (opens new window)

PandasでPostgreSQLを操作する

PandasでPostgreSQLを操作する

PandasでPostgreSQLを操作します.

Python + seleniumでポケモンの画像をウェブスクレイピングする

Python + seleniumでポケモンの画像をウェブスクレイピングする

Python + seleniumでポケモンの画像をウェブスクレイピングで収集します.