将频率转换为 -1..1 浮点数 [英] Converting a frequency into a -1..1 float

查看:50
本文介绍了将频率转换为 -1..1 浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 (C#) XNA 音频框架的 SoundEffect 类以给定的音高播放声音.到目前为止,我有这个(非常基本的)

I'm trying to play back a sound at a given pitch using (C#) XNA audio framework's SoundEffect class. So far, I have this (very basic)

public void playSynth(SoundEffect se, int midiNote) {
    float pitch = (float)functions.getMIDIFreq(midiNote) / ((float)functions.getMIDIFreq(127)/2);
    pitch-=1F;
    Debug.WriteLine("Note: {0}. Float: {1}",midiNote,pitch);
    synth = se.CreateInstance();
    synth.IsLooped = false;
    synth.Pitch = pitch;
    synth.Play();
}

目前,播放的音高非常离键,因为数学是错误的.这个函数的工作方式是我使用一个名为 getMIDIFreq 的函数向该函数发送一个 MIDI 音符(0 到 127) 将该音符转换为频率 - 效果很好.

Currently, the pitch played back is very off-key, because the math is wrong. The way this function works is I'm sending a MIDI note (0 through 127) to the function, using a function I made called getMIDIFreq to convert that note to a frequency - which works fine.

为了调用这个函数,我使用了这个:

To call this function, I'm using this:

SoundEffect sound = SoundEffect.FromStream(TitleContainer.OpenStream(@"synth.wav"));
playSynth(sound,(int)midiNote);  //where midiNote is 0...127 number

其中 synth.wav 是我在 DAW 中创建并导出的简单 C 音符.该程序的全部意义在于播放该合成器声音中给出的 MIDI 音符,但我很乐意接受正弦波或其他任何东西.我无法使用 Console.Beep 因为它非常慢而且不能快速连续播放带有音符的整首歌曲.

where synth.wav is a simple C note I created in a DAW and exported. The whole point of this program is to play back the MIDI note given in that synth sound, but I'd gladly settle for a sine wave, or anything really. I can't use Console.Beep because it's extremely slow and not for playing entire songs with notes in rapid succession.

所以我的问题是,我该如何修复这段代码,让它以正确的音高播放样本?我意识到我在这里只有 2 个八度音程可以使用,所以如果有一个解决方案可以在给定的频率下生成一个音调并且速度非常快,那就更好了.

So my question is, how could I fix this code so it plays the sample at the right pitch? I realize I only have 2 octaves to work with here, so if there's a solution that involves generating a tone at a given frequency and is very fast, that would be even better.

谢谢!

我将其设为 WinForms 应用程序,而不是 XNA 游戏,但我已下载并在我的项目中引用了该框架.

I'm making this a WinForms application, not an XNA game, but I have the framework downloaded and referenced in my project.

推荐答案

您不能应用任意频率.您只能将音高降低一个八度(半频)或将其提高一个八度(双频).因此,要计算弯音值,首先需要知道样本的初始音高.

You can't apply an arbitrary frequency. You can only lower pitch by an octave (half frequency) or raise it by an octave (double frequency). So, to calculate the pitch bend value, you first need to know the initial pitch of the sample.

假设您的样本是 440 Hz A,并且您希望 A 向下一个八度音程 (220 Hz).您需要的值是 -1.yourPitch/initiPitch = 0.52.0.你需要把它变成 -1+1 的比例.我不能确切地告诉你如何,因为文档不清楚尺度是否是对数的.您必须进行实验,但这应该会让您开始.

Suppose your sample is 440 Hz A, and you want an A an octave down (220 Hz). The value you need is -1. yourPitch / initiPitch = 0.5 to 2.0. You will need to make that into the scale of -1 to +1. I can't tell you exactly how, because the documentation isn't clear if the scale is logarithmic or not. You would have to experiment, but this should get you started.

这篇关于将频率转换为 -1..1 浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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