Tone Generation in Cocoa Touch

2019-01-21 18:09发布

I need to generate a tone that I can manipulate frequency and wave. The overall goal is to create a basic piano. Does anyone know how I can achieve this?

My development platform is the iPhone 2.x

6条回答
爷的心禁止访问
2楼-- · 2019-01-21 18:22

You could always start with sin waves. :-)

#include <cmath>

typedef double Sample;
typedef double Time;

class MonoNote {
protected:
    Time start, duration;
    virtual void internalRender(double now, Sample *mono) = 0;
public:
    MonoNote(Time s, Time d) : start(s), duration(d) {}
    virtual ~MonoNote() {}
    void render(double now, Sample *mono) {
        if (start <= now && now < start + duration) {
            internalRender(now, mono);
        }
    }
};

class MonoSinNote : public MonoNote {
    Time freq;
    Sample amplitude;
protected:
    void internalRender(double now, Sample *mono) {
        const double v = sin(2*M_PI*(now - start) * freq);
        *mono += amplitude*v;
    }
public:
    MonoSinNote(Time s, Time d, Time f, Sample a) : MonoNote(s, d), freq(f), amplitude(a) {}
    ~MonoSinNote() {}
};
查看更多
够拽才男人
3楼-- · 2019-01-21 18:27

Piano is strange. Robert Moog wrote about it in Keyboard Magazine in March 1980. The fundamental (lowest frequency partial) is in tune, but each higher harmonic is brighter (or "sharper" or higher-pitched) than it should be, and by an increasing amount.

The second through ninth harmonics are louder than the fundamental. The tenth through twentieth are about as loud.

The fundamental swells up in volume and then dives, then it comes back. The other partials have characteristic up and down shapes. The partials exchange energy so the overall volume acts as you would expect. Buts it's a swarm of partials trading energy. I'd guess if you got the lowest few right and the weird inharmonic spread right you'd do OK.

You could watch the action in a software spectrum analyzer and learn what you need to know. Additive synthesis is probably how I'd take on the problem.

查看更多
Animai°情兽
4楼-- · 2019-01-21 18:32

Check out the DefaultOutputUnit sample code which plays a sine wave.

/Developer/Examples/CoreAudio/SimpleSDK/DefaultOutputUnit

查看更多
狗以群分
5楼-- · 2019-01-21 18:34

Apple Developer Forums has a thread on this ("Audio Synthesis") that might provide some insight.

查看更多
We Are One
6楼-- · 2019-01-21 18:44

Check out http://mda.smartelectronix.com/. They are a series of open source VST plugins. Look at the source for Piano, ePiano or DX10. It's about as simple as you are going to find.

查看更多
Rolldiameter
7楼-- · 2019-01-21 18:45

Check out Mobilesynth...an open source synthesizer in the app store: http://code.google.com/p/mobilesynth/

查看更多
登录 后发表回答