[C#] 任意频率 PCM 正弦波生成与 WAV 格式文件保存

封装一个简单的 PcmBuilder, 以 IEnumerable<float> 的形式提供采样值:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class PcmBuilder
{
    private PcmBuilder() { }

    public float SampleRate { get; set; } = 44100;
    public float Frequency { get; set; } = 4000;
    public float Amplitude { get; set; } = 1;

    public PcmBuilder WithSampleRate(long sampleRate)
    {
        SampleRate = sampleRate;
        return this;
    }

    public PcmBuilder WithFrequency(float frequency)
    {
        Frequency = frequency;
        return this;
    }

    public PcmBuilder WithAmplitude(float amplitude)
    {
        Amplitude = amplitude;
        return this;
    }

    public IEnumerable<float> Build()
    {
        float current = 0;
        while (true)
        {
            yield return MathF.Sin(current / SampleRate * Frequency) * Amplitude;

            current++;
        }
    }

    public static PcmBuilder CreateNew() => new PcmBuilder();
}

使用它构建出 44100Hz 采样率, 8000Hz 的正弦波:

1
2
3
4
var pcm = PcmBuilder.CreateNew()
    .WithSampleRate(44100)
    .WithFrequency(8000)
    .Build();

WAV 文件的头格式:

WAV 文件的头结构体定义:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
public unsafe struct WaveHeader
{
    private fixed byte _chunkId[4];
    private uint _chunkSize;
    private fixed byte _format[4];

    private fixed byte _subChunk1Id[4];
    private uint _subChunk1Size;
    private WaveAudioFormat _audioFormat;
    private ushort _numChannels;
    private uint _sampleRate;
    private uint _byteRate;
    private ushort _blockAlign;
    private ushort _bitsPerSample;

    private fixed byte _subChunk2Id[4];
    private uint _subChunk2Size;

    public unsafe string ChunkId
    {
        get
        {
            fixed (byte* ptr = _chunkId)
            {
                return CreateString(ptr, 4);
            }
        }
        set
        {
            fixed (byte* ptr = _chunkId)
            {
                FillString(ptr, value, 4);
            }
        }
    }

    public uint ChunkSize
    {
        get => _chunkSize;
        set => _chunkSize = value;
    }

    public string Format
    {
        get
        {
            fixed (byte* ptr = _format)
            {
                return CreateString(ptr, 4);
            }
        }
        set
        {
            fixed (byte* ptr = _format)
            {
                FillString(ptr, value, 4);
            }
        }
    }

    public string SubChunk1Id
    {
        get
        {
            fixed (byte* ptr = _subChunk1Id)
            {
                return CreateString(ptr, 4);
            }
        }
        set
        {
            fixed (byte* ptr = _subChunk1Id)
            {
                FillString(ptr, value, 4);
            }
        }
    }

    public uint SubChunk1Size
    {
        get => _subChunk1Size;
        set => _subChunk1Size = value;
    }

    public WaveAudioFormat AudioFormat
    {
        get => _audioFormat;
        set => _audioFormat = value;
    }

    public ushort ChannelCount
    {
        get => _numChannels;
        set => _numChannels = value;
    }

    public uint SampleRate
    {
        get => _sampleRate;
        set => _sampleRate = value;
    }

    public uint ByteRate
    {
        get => _byteRate;
        set => _byteRate = value;
    }

    public ushort BlockAlign
    {
        get => _blockAlign;
        set => _blockAlign = value;
    }

    public ushort BitsPerSample
    {
        get => _bitsPerSample;
        set => _bitsPerSample = value;
    }

    public string SubChunk2Id
    {
        get
        {
            fixed (byte* ptr = _subChunk2Id)
            {
                return CreateString(ptr, 4);
            }
        }
        set
        {
            fixed (byte* ptr = _subChunk2Id)
            {
                FillString(ptr, value, 4);
            }
        }
    }

    public uint SubChunk2Size
    {
        get => _subChunk2Size;
        set => _subChunk2Size = value;
    }

    public unsafe static WaveHeader Create(WaveAudioFormat audioFormat, ushort channelCount, uint sampleRate, ushort bitsPerSample, uint pcmDataSize)
    {
        return new WaveHeader()
        {
            ChunkId = "RIFF",
            ChunkSize = (uint)(pcmDataSize + (sizeof(WaveHeader) - 8)),
            Format = "WAVE",

            SubChunk1Id = "fmt ",
            SubChunk1Size = 16,
            AudioFormat = audioFormat,
            ChannelCount = channelCount,
            SampleRate = sampleRate,
            ByteRate = sampleRate * channelCount * bitsPerSample / 8,
            BlockAlign = (ushort)(channelCount * bitsPerSample / 8),
            BitsPerSample = bitsPerSample,

            SubChunk2Id = "data",
            SubChunk2Size = pcmDataSize
        };
    }

    private static void FillString(byte* ptr, string value, int maxLength)
    {
        if (value.Length > maxLength)
        {
            throw new ArgumentException(nameof(value));
        }

        fixed (char* textPtr = value)
        {
            for (int i = 0; i < value.Length && i < maxLength; i++)
            {
                ptr[i] = (byte)textPtr[i];
            }
        }
    }

    private static string CreateString(byte* ptr, int maxLength)
    {
        StringBuilder sb = new StringBuilder(maxLength);
        for (int i = 0; i < maxLength; i++)
        {
            if (ptr[i] == 0)
            {
                break;
            }

            sb.Append((char)ptr[i]);
        }

        return sb.ToString();
    }
}

public enum WaveAudioFormat : ushort
{
    None = 0,
    PCM = 1,
}

使用它构建 PCM 格式, 单声道, 44100Hz 采样率, 32位深, 30 秒的音频 WAV 头:

1
var header = WaveHeader.Create(WaveAudioFormat.PCM, 1, 44100, 32, 44100 * 4 * 30);

创建文件, 并写入 WAV 头和 PCM 内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using var output = File.Create("output.wav");
using var outputWriter = new BinaryWriter(output);

var pcm = PcmBuilder.CreateNew()
    .WithSampleRate(44100)
    .WithFrequency(8000)
    .Build();

var header = WaveHeader.Create(WaveAudioFormat.PCM, 1, 44100, 32, 44100 * 4 * 30);

// 写入 Header
unsafe
{
    var headerByteSpan = new Span<byte>((byte*)&header, sizeof(WaveHeader));
    outputWriter.Write(headerByteSpan);
}

// 获取 PCM 数据迭代器
var pcmEnumerator = pcm.GetEnumerator();

// 向文件写入 PCM
for (int i = 0; i < 44100 * 30; i++)
{
    pcmEnumerator.MoveNext();

    outputWriter.Write(pcmEnumerator.Current);
}
Built with Hugo
主题 StackJimmy 设计