问题描述
除了Console.Beep方法之外,是否还有其他方法可以使您产生音调?要使哔哔声从一个频率开始但逐渐从另一个频率结束?
Is there a way to make real-time tones other than the Console.Beep method that let's you slur notes? To make the beep start at one frequency but gradually end at a different one?
推荐答案
您可以使用JohnWein的BeepBeep:
you can use JohnWein's BeepBeep for it:
https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral
https://social.msdn.microsoft.com/Forums/vstudio/en-US/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral
修改它以增加循环中的频率...
Modify it to increment the frequency in the loop...
需要在代码文件的顶部:
needed at the top of the code file:
使用System.IO;
使用System.Media;
using System.IO;
using System.Media;
然后例如:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
//protect your ears, leave the amplitude low while testing.
Beep.BeepBeep(100, 500, 2000);
}
}
public class Beep
{
//orig by JohnWein, https://social.msdn.microsoft.com/Forums/vstudio/en-US/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral
public static void BeepBeep(int Amplitude, double Frequency, int Duration)
{
double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
double DeltaFT = 2 * Math.PI * Frequency / 44100.0;
int Samples = 441 * Duration / 10;
double increment = Frequency / (double)Samples; //be careful with setting this, might hurt your ears when producing chirp sounds, so dont use with ear buds in.
int Bytes = Samples * 4;
int[] Hdr = { 0X46464952, 36 + Bytes, 0X45564157, 0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 0X61746164, Bytes };
using (MemoryStream MS = new MemoryStream(44 + Bytes))
{
using (BinaryWriter BW = new BinaryWriter(MS))
{
for (int I = 0; I < Hdr.Length; I++)
{
BW.Write(Hdr[I]);
}
for (int T = 0; T < Samples; T++)
{
Frequency += increment;
DeltaFT = 2 * Math.PI * Frequency / 44100.0;
short Sample = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T));
BW.Write(Sample);
BW.Write(Sample);
}
BW.Flush();
MS.Seek(0, SeekOrigin.Begin);
using (SoundPlayer SP = new SoundPlayer(MS))
{
SP.PlaySync();
}
}
}
}
}
此致
托尔斯滕
这篇关于语调不清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!