如何制作和播放程序生成的线性调频声音

发布于 2024-11-01 09:12:21 字数 116 浏览 1 评论 0原文

我的目标是创建一个“Chirper”课程。鸣叫器应该能够发出按程序生成的鸣叫声。具体想法是,鸣叫声必须按程序生成,而不是回放预先录制的声音。

在 iPhone 上实现程序生成的鸣叫声的最简单方法是什么?

My goal is to create a "Chirper" class. A chirper should be able to emit a procedurally generated chirp sound. The specific idea is that the chirp must be procedurally generated, not a prerecorded sound played back.

What is the simplest way to achieve a procedurally generated chirp sound on the iPhone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-11-08 09:12:21

正如您所说,您可以使用正弦波来做到这一点,您可以使用 sin 函数来定义它。只要您想要样本中的声音,就创建一个缓冲区,例如:

// 1 second chirp
float samples[44100];

然后选择一个起始频率和结束频率,您可能希望起始频率高于结束频率,例如:

float startFreq = 1400;
float endFreq = 1100;

float thisFreq;
int x;
for(x = 0; x < 44100; x++)
{
    float lerp = float(float(x) / 44100.0);

    thisFreq = (lerp * endFreq) + ((1 - lerp) * startFreq);
    samples[x] = sin(thisFreq * x);
}

无论如何,类似的东西。

如果您想要嗡嗡声或其他声音,请使用不同的波形 - 创建它们以与 sin 非常相似的方式工作,并且您可以互换使用它们。这样你就可以创建 saw() sqr() tri(),并且你可以做一些事情,比如将它们组合起来形成更复杂或更多样化的声音

==================== ====

编辑 -

如果你想玩,你应该能够使用 OpenAL 做一些类似的事情。重要的是使用 OpenAL 或类似的 iOS API 来播放原始缓冲区。

    alGenBuffers (1, &buffer); 
    alBufferData (buffer, AL_FORMAT_MONO16, buf, size, 8000); 
    alGenSources (1, &source); 

    ALint state; 

    // attach buffer and play 
    alSourcei (source, AL_BUFFER, buffer); 
    alSourcePlay (source); 

    do 
    { 
        wait (200); 
        alGetSourcei (source, AL_SOURCE_STATE, &state); 
    } 
    while ((state == AL_PLAYING) && play); 

    alSourceStop(source); 
    alDeleteSources (1, &source); 

    delete (buf) 
} 

You can do it with a sine wave as you said, which you would define using the sin functions. Create a buffer as long as you want the sound in samples, such as:

// 1 second chirp
float samples[44100];

Then pick a start frequency and end frequency, which you probably want the start to be higher than the end, something like:

float startFreq = 1400;
float endFreq = 1100;

float thisFreq;
int x;
for(x = 0; x < 44100; x++)
{
    float lerp = float(float(x) / 44100.0);

    thisFreq = (lerp * endFreq) + ((1 - lerp) * startFreq);
    samples[x] = sin(thisFreq * x);
}

Something like that, anyway.

And if you want a buzz or another sound, use different waveforms - create them to work very similarly to sin and you can use them interchangably. That way you could create saw() sqr() tri(), and you could do things like combine them to form more complex or varied sounds

========================

Edit -

If you want to play you should be able to do something along these lines using OpenAL. The important thing is to use OpenAL or a similar iOS API to play the raw buffer.

    alGenBuffers (1, &buffer); 
    alBufferData (buffer, AL_FORMAT_MONO16, buf, size, 8000); 
    alGenSources (1, &source); 

    ALint state; 

    // attach buffer and play 
    alSourcei (source, AL_BUFFER, buffer); 
    alSourcePlay (source); 

    do 
    { 
        wait (200); 
        alGetSourcei (source, AL_SOURCE_STATE, &state); 
    } 
    while ((state == AL_PLAYING) && play); 

    alSourceStop(source); 
    alDeleteSources (1, &source); 

    delete (buf) 
} 
安穩 2024-11-08 09:12:21

您可以在远程 I 的渲染回调中使用 Nektarios 的代码/O 单位。您甚至可以实时更改波形(低延迟)。

You can use Nektarios's code in the render callback for remote I/O unit. Also you can even change waveforms real-time (low latency).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文