如何制作和播放一个程序产生的啁啾声 [英] How to Make and Play A Procedurally Generated Chirp Sound

查看:198
本文介绍了如何制作和播放一个程序产生的啁啾声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是建立一个Chirper级。一个chirper应该能够发出一个程序产生的啁啾声。具体想法是,啁啾必须程序产生的,而不是一个prerecorded声音回放。

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.

什么是实现对iPhone?

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

推荐答案

您可以像你说的正弦波,您将定义使用罪的功能做到这一点。创建一个缓冲,只要你想的样品,如声音:

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.

如果你想嗡嗡声或其他声音,使用不同的波形 - 创建它们非常相似工作,以犯罪,你可以互换使用。这样,你可以创建锯()SQR()三(),你可以做这样的事情它们组合成更复杂或多变的声音

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

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

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

编辑 -

如果你要玩,你应该能够做到一起使用OpenAL的这些路线的东西。最重要的是使用OpenAL的或类似的iOS API来播放原始缓冲区。

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) 
} 

这篇关于如何制作和播放一个程序产生的啁啾声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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