如何根据频率产生声音? [英] How to generate sounds according to frequency?

查看:253
本文介绍了如何根据频率产生声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  创建正弦波或方波在C#

我要生成的声音。要么是这样的:

I want to generate sounds. Either something like:

MakeSound(frequency, duration);

或者

MakeSound(MyArrayOfSamples);

我已经发现了一些所谓的: Microsoft.Directx.DirectSound ,但看过它已经停产。我找不到它作为在Visual Studio(2010年)的参考选项。我发现按照此链接我读过,应该包括它,但我不敢随便使用的东西,从2006年,因为它可能不再被支持。和这个说,这是对C / C ++(尽管也说:管理code),我不想浪费几周试图了解如何包装的成管理code,只是为了找出我不能做到这一点。我发现的最有希望的链接的 WAVEFORMAT ,但我无法找到如何使用它。

I've found something called: Microsoft.Directx.DirectSound but have read that it has been discontinued. And I couldn't find it as an option for a reference in Visual Studio (2010). I've found this link which according to what I've read, is supposed to include it, but am hesitant to use something from 2006 since it might not be supported anymore. And this one says it's for C/C++ (despite also saying: "managed code") and I don't want to waste weeks trying to understand how to wrap that into managed code, just to find out I can't do it. The most promising link I've found is WaveFormat but I couldn't find how to use it.

我的没有的询问如何获得数学再presentation的声波。我也不问如何播放MP3文件等。我也不是找第三方软件或包装。只是针对一个非常具体的目标,一个C#/。NET解决方案。

I'm not asking how to get the mathematical representation for the sound wave. Nor am I asking how to play an mp3 file or the like. Nor am I looking for third party software or wrappers. Just for a C# / .net solution for a very specific objective.

推荐答案

无论哪种方式,你看它,除非你想使用的非托管code,你将不得不建立一个WAV播放它。然而,下面是从<一个一个code段href="http://blogs.msdn.com/b/ericlippert/archive/2005/04/15/desafinado-part-four-rolling-your-own-wav-files.aspx">Eric利珀特的博客,会告诉你如何使用的频率推出自己的WAV文件。

Either way you look at it, unless you want to used unmanaged code, you're going to have to build a WAV to play it. However, below is a code snippet from Eric Lippert's blog that will show you how to roll your own WAV file using frequencies.

namespace Wave
{
   using System;
   using System.IO;
   class MainClass {
      public static void Main() {
         FileStream stream = new FileStream("test.wav", FileMode.Create);
         BinaryWriter writer = new BinaryWriter(stream);
         int RIFF = 0x46464952;
         int WAVE = 0x45564157;
         int formatChunkSize = 16;
         int headerSize = 8;
         int format = 0x20746D66;
         short formatType = 1;
         short tracks = 1;
         int samplesPerSecond = 44100;
         short bitsPerSample = 16;
         short frameSize = (short)(tracks * ((bitsPerSample + 7)/8));
         int bytesPerSecond = samplesPerSecond * frameSize;
         int waveSize = 4;
         int data = 0x61746164;
         int samples = 88200 * 4;
         int dataChunkSize = samples * frameSize;
         int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
         writer.Write(RIFF);
         writer.Write(fileSize);
         writer.Write(WAVE);
         writer.Write(format);
         writer.Write(formatChunkSize);
         writer.Write(formatType);
         writer.Write(tracks); 
         writer.Write(samplesPerSecond);
         writer.Write(bytesPerSecond);
         writer.Write(frameSize);
         writer.Write(bitsPerSample); 
         writer.Write(data);
         writer.Write(dataChunkSize);
         double aNatural = 220.0;
         double ampl = 10000;
         double perfect = 1.5;
         double concert = 1.498307077;
         double freq = aNatural * perfect;
         for (int i = 0; i < samples / 4; i++) {
            double t = (double)i / (double)samplesPerSecond;
            short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI)));
            writer.Write(s);
         }
         freq = aNatural * concert;
         for (int i = 0; i < samples / 4; i++) {
            double t = (double)i / (double)samplesPerSecond;
            short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI)));
            writer.Write(s);
         }
         for (int i = 0; i < samples / 4; i++) {
            double t = (double)i / (double)samplesPerSecond;
            short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI) + Math.Sin(t * freq * perfect * 2.0 * Math.PI)));
            writer.Write(s);
         }
         for (int i = 0; i < samples / 4; i++) {
            double t = (double)i / (double)samplesPerSecond;
            short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI) + Math.Sin(t * freq * concert * 2.0 * Math.PI)));
            writer.Write(s);
         }
         writer.Close();
         stream.Close();
      }
   }
}

打破它除了为您的需求,但要注意的一个自然变量 - 这是一个频率 - 就像你在寻找什么

Break it apart for your needs, but notice the aNatural variable - it's a frequency - just like what you're looking for.

现在,你可以放置到一个的MemoryStream 然后用声音播放玩,如果你喜欢

Now, you can place that into a MemoryStream and then play it with SoundPlayer if you like.

这篇关于如何根据频率产生声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆