我使用http://soundfile.sapp.org/doc/WaveFormat/中的信息创建了一个音频合成器。
private const int SAMPLE_RATE = 44100;
private const short BITS_PER_SAMPLE = 16;
short[] asSoundValues = new short[SAMPLE_RATE]
float fFreq = 440;
for (int iValueCount = 0; iValueCount < SAMPLE_RATE; iValueCount++)
{
asSoundValues[iValueCount] = Convert.ToInt16(short.MaxValue *
Math.Sin(((Math.PI * 2 * fFreq) / SAMPLE_RATE) * iValueCount));
}
byte[] abBinarySoundValues = new byte[SAMPLE_RATE * sizeof(short)];
Buffer.BlockCopy(asSoundValues, 0, abBinarySoundValues, 0,
asSoundValues.Length * sizeof(short));
using (MemoryStream msMemoryStream = new MemoryStream())
{
using (BinaryWriter bwBinaryWriter = new BinaryWriter(msMemoryStream))
{
short sBlockAlign = BITS_PER_SAMPLE / 8;
int iSubChunkTwoSize = SAMPLE_RATE * sBlockAlign;
bwBinaryWriter.Write(new[] { 'R', 'I', 'F', 'F' });
bwBinaryWriter.Write(36 + iSubChunkTwoSize);
bwBinaryWriter.Write(new[] { 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ' });
bwBinaryWriter.Write(16);
bwBinaryWriter.Write((short)1);
bwBinaryWriter.Write((short)1);
bwBinaryWriter.Write(SAMPLE_RATE);
bwBinaryWriter.Write(SAMPLE_RATE * sBlockAlign);
bwBinaryWriter.Write(sBlockAlign);
bwBinaryWriter.Write(BITS_PER_SAMPLE);
bwBinaryWriter.Write(new[] { 'd', 'a', 't', 'a' });
bwBinaryWriter.Write(iSubChunkTwoSize);
bwBinaryWriter.Write(abBinarySoundValues);
msMemoryStream.Position = 0;
new SoundPlayer(msMemoryStream).Play();
}
}
声音持续1秒钟。如何将声音延长到更长的时间-例如5秒? 最佳答案
根据wave format,Subchunk2Size
最终决定了WAV的长度,这有助于我弄清楚除了样本数组之外所有必须更改的东西。因此,这些都是您必须进行的更改:
for (int iValueCount = 0; iValueCount < SAMPLE_RATE * numberOfSeconds; iValueCount++)
short[] asSoundValues = new short[SAMPLE_RATE * numberOfSeconds]
byte[] abBinarySoundValues = new byte[SAMPLE_RATE * sizeof(short) * numberOfSeconds];
int iSubChunkTwoSize = SAMPLE_RATE * sBlockAlign * numberOfSeconds;
关于c# - 如何延长音频合成器的时间长度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64448379/