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?

More than 3 years have passed since last update.

音階波形の生成

Last updated at Posted at 2022-08-05

環境

Unity2021.3.4f1

概要

プロシージャルに音階の波形を生成して、かえるのうたを流してみました。
このままだとノイズがひどいので、AudioMixerを作成してLowpassフィルタを追加して値調整するとノイズが軽減されます。

OnAudioFilterReadはAudioスレッドから呼ばれています。
AudioSourceにAudioClipを設定しなくても、第1引数のdataに値を設定するとその音が鳴ります。

ここに全コード記載しています。

private void OnAudioFilterRead(float[] data, int channels)
{
    ...
    for (int i = 0; i< data.Length; i++)
    {
        float time = begin + (float)currentSampleIndex / _outputSampleRate;
        data[i] = volume * Mathf.Sin(2.0f * Mathf.PI * time * note.tone);
        currentSampleIndex++;
        if (channels == 2)
        {
            data[i + 1] = data[i];
            i++;
        }
    }
}

参考

https://tomari.org/main/java/oto.html
https://fabeee.co.jp/column/employee-blog/sugichan02/

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?