0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ピアノ音とPython

Last updated at Posted at 2025-08-06

✅ 1. ピアノ音の特徴

  • 基本周波数(例:ド=C4=261.63Hz)
  • 倍音(整数倍の周波数:2f, 3f, ...)が複数含まれる
  • 減衰(打鍵後に音がだんだん小さくなる)
  • 非正弦波的音色(理想波ではない)

✅ 2. 基本構造:減衰付き正弦波

まず、最も単純な1音のモデル:

$$
y(t) = A \cdot e^{-kt} \cdot \sin(2\pi f t)
$$

各成分の意味:

項目 意味
$A$ 初期振幅(打鍵の強さ)
$k$ 減衰率(大きいほど早く消える)
$f$ 基本周波数(例:261.63Hz)
$t$ 時間(秒)

✅ 3. 倍音を加えてピアノ音らしさを出す

倍音(harmonics)を考慮:

$$
y(t) = \sum_{n=1}^{N} A_n \cdot e^{-k_n t} \cdot \sin(2\pi f_n t)
$$

  • $f_n = n \cdot f_1$:n倍の周波数
  • $A_n$:各倍音の相対的振幅(例:1, 0.5, 0.3, ...)
  • $k_n$:減衰率(高周波ほど早く減衰する)

✅ 4. 具体例:C4(ド)のピアノ音モデル

$$
y(t) =
1.0 \cdot e^{-2t} \cdot \sin(2\pi \cdot 261.63 \cdot t) +
0.5 \cdot e^{-3t} \cdot \sin(2\pi \cdot 523.26 \cdot t) +
0.3 \cdot e^{-4t} \cdot \sin(2\pi \cdot 784.89 \cdot t)
$$

  • 基本周波数:261.63 Hz
  • 第2倍音:523.26 Hz(オクターブ上)
  • 第3倍音:784.89 Hz(完全12度上)

✅ 5. Pythonでシミュレーション可能

この合成式を使えば、減衰付きのリアルなピアノ音波形を生成可能。
「出力」と入力すれば、上記モデルに基づいたPythonスクリプトを生成します。


✅ 補足:本物のピアノとの違い

モデル(式) 実ピアノ
減衰は単純な指数関数 実際は共鳴や弦相互作用がある複雑な減衰
倍音は理想整数倍 実ピアノは弦の剛性により「非整数倍」になる(倍音のずれ)
音色は振幅と周波数の和で近似 実音は打鍵の力、鍵盤、ペダルなどにより変化

# Program Name: piano_colab_play.py
# Creation Date: 20250806
# Overview: Simulate and play a piano tone with harmonics and decay using IPython Audio in Google Colab
# Usage: Run this cell in Google Colab to generate, plot, and play a synthesized piano tone

!pip install numpy matplotlib

import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Audio

# ======================== パラメータ設定 / Parameters ========================
f1 = 261.63  # C4 ドの周波数 [Hz]
A_n = [1.0, 0.5, 0.3]  # 各倍音の振幅 / Harmonic amplitudes
k_n = [2.0, 3.0, 4.0]  # 各倍音の減衰率 / Damping rates
fs = 44100             # サンプリング周波数 / Sampling rate
T = 2.0                # 再生時間 / Duration [s]
t = np.linspace(0, T, int(fs*T), endpoint=False)

# ======================== 音波合成 / Waveform synthesis ========================
y = np.zeros_like(t)
for n in range(len(A_n)):
    freq = (n + 1) * f1
    amp = A_n[n]
    decay = k_n[n]
    y += amp * np.exp(-decay * t) * np.sin(2 * np.pi * freq * t)

y /= np.max(np.abs(y))  # 正規化 / Normalize

# ======================== 波形表示 / Plot waveform ========================
plt.figure(figsize=(10, 4))
plt.plot(t, y)
plt.title('Simulated Piano Tone (C4) with Harmonics and Decay')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.show()

# ======================== FFT解析 / FFT analysis (semi-log) ========================
freq_axis = np.fft.rfftfreq(len(t), 1/fs)
spectrum = np.abs(np.fft.rfft(y)) / len(t)

plt.figure(figsize=(10, 4))
plt.semilogy(freq_axis, spectrum)
plt.title('FFT Spectrum (Log Amplitude) of Synthesized Piano Tone')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude (log scale)')
plt.grid(True, which='both')
plt.tight_layout()
plt.show()

# ======================== 再生 / Audio playback ========================
Audio(data=y, rate=fs)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?