private void Beep(int frequency) { using (MemoryStream memoryStream = new MemoryStream(0x24 + bytes)) { memoryStream.Seek(0L, SeekOrigin.Begin); using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream)) { signalGenerator.Amplitude = short.MaxValue; signalGenerator.Frequency = frequency; for (int i = 0; i <= header.Length - 1; i++) { binaryWriter.Write(header[i]); } for (int i = 0; i <= samples - 1; i++) { binaryWriter.Write(Convert.ToInt16(signalGenerator.GetValue(i * tick))); } binaryWriter.Flush(); memoryStream.Seek(0L, SeekOrigin.Begin); soundPlayer.Stream = memoryStream; soundPlayer.PlaySync(); } } }
public void SaveBeepFile(int frequency, int duration, string fileName) { float Tick = 1f / 44100f; double durationDouble = this.RoundDurationToFrequencyCycle(duration, frequency); int Samples = (int)Math.Round((44100.0 * durationDouble) / 1000.0); //int Samples = (44100 * duration) / 1000; int Bytes = Samples * sizeof(short); int[] Header = new int[] { 0x46464952, 0x24 + Bytes, 0x45564157, 0x20746d66, 0x10, 0x10001, 0xac44, 0x15880, 0x100002, 0x61746164, Bytes }; using (MemoryStream memoryStream = new MemoryStream(0x24 + Bytes)) { using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream)) { SignalGenerator signalGenerator = new SignalGenerator(); signalGenerator.Amplitude = short.MaxValue; signalGenerator.Frequency = frequency; for (int i = 0; i <= Header.Length - 1; i++) { binaryWriter.Write(Header[i]); } for (int i = 0; i <= Samples - 1; i++) { binaryWriter.Write(Convert.ToInt16(signalGenerator.GetValue(i * Tick))); } binaryWriter.Flush(); memoryStream.Seek(0L, SeekOrigin.Begin); using (FileStream file = new FileStream(fileName, FileMode.Create, System.IO.FileAccess.Write)) { byte[] bytes = new byte[memoryStream.Length]; memoryStream.Read(bytes, 0, (int)memoryStream.Length); file.Write(bytes, 0, bytes.Length); memoryStream.Close(); } } } }
public void Beep(int frequency, int duration) { float Tick = 1f / 44100f; int Samples = (44100 * duration) / 1000; int Bytes = Samples * sizeof(short); int[] Header = new int[] { 0x46464952, 0x24 + Bytes, 0x45564157, 0x20746d66, 0x10, 0x10001, 0xac44, 0x15880, 0x100002, 0x61746164, Bytes }; using (MemoryStream memoryStream = new MemoryStream(0x24 + Bytes)) { using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream)) { SignalGenerator signalGenerator = new SignalGenerator(); signalGenerator.Amplitude = short.MaxValue; signalGenerator.Frequency = frequency; for (int i = 0; i <= Header.Length - 1; i++) { binaryWriter.Write(Header[i]); } for (int i = 0; i <= Samples - 1; i++) { binaryWriter.Write(Convert.ToInt16(signalGenerator.GetValue(i * Tick))); } binaryWriter.Flush(); memoryStream.Seek(0L, SeekOrigin.Begin); using (SoundPlayer soundPlayer = new SoundPlayer(memoryStream)) { soundPlayer.PlaySync(); } } } }