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

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...