Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

iphone - Tone Generation in Cocoa Touch

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() {}
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...