Initial commit
[camargo/neiasound.git] / src / nSoundSource.h
1 #ifndef NSOUNDSOURCE_H
2 #define NSOUNDSOURCE_H
3
4 #include <QObject>
5 #include <QVector3D>
6 #include "nSoundSourceRole.h"
7
8 class nSoundSystem;
9 class nSoundBuffer;
10
11
12 class nSoundSource : public QObject
13 {
14     Q_OBJECT
15     Q_ENUMS(nSoundSourceState)
16     Q_ENUMS(nSoundSourceRole)
17
18     Q_PROPERTY(unsigned int openalHandle READ openalHandle)
19     Q_PROPERTY(nSoundSourceRole role READ role)
20     Q_PROPERTY(nSoundSourceState state READ state)
21     Q_PROPERTY(qreal gain READ gain WRITE setGain)
22     Q_PROPERTY(qreal pitch READ pitch WRITE setPitch)
23     Q_PROPERTY(qreal rolloffFactor READ rolloffFactor WRITE setRolloffFactor)
24     Q_PROPERTY(bool loop READ loop WRITE setLoop)
25     Q_PROPERTY(QVector3D position READ position WRITE setPosition)
26     Q_PROPERTY(bool destroyAfterStopped READ destroyAfterStopped WRITE setDestroyAfterStopped NOTIFY onDestroyAfterStoppedChanged )
27
28     Q_PROPERTY(qreal fading READ isFading)
29     Q_PROPERTY(qreal fadeTarget READ fadeTarget)
30     Q_PROPERTY(qreal fadeDeltaPerSecond READ fadeDeltaPerSecond)
31
32
33 public:
34     enum nSoundSourceState
35     {
36         SSS_UNKNOWN,
37         SSS_PLAYING,
38         SSS_PAUSED,
39         SSS_STOPPED,
40         SSS_INITIAL
41     };
42
43     explicit nSoundSource(QString name, nSoundSourceRole role, nSoundSystem * parent);
44     virtual ~nSoundSource();
45
46     unsigned int openalHandle(){return m_handle;}
47     nSoundSourceState state();
48     nSoundSourceRole role(){return m_role;}
49     qreal gain();
50     qreal pitch();
51     qreal rolloffFactor();
52     bool loop();
53     QVector3D position();
54
55     bool isFading() const {return m_fading;}
56     qreal fadeTarget() const {return m_fadeTarget;}
57     qreal fadeDeltaPerSecond() const {return m_fadeDeltaPerSecond;}
58
59     // GAIN CFG SUPPORT
60     static qreal _roleGainVolume(nSoundSourceRole role);
61     static void _resetRoleGains();
62
63     bool destroyAfterStopped() const
64     {
65         return m_destroyAfterStopped;
66     }
67
68 signals:
69
70     void onDestroyAfterStoppedChanged(bool arg);
71
72 public slots:
73     bool update(qreal frameTime);
74
75     void attachBuffer(nSoundBuffer * buffer);
76
77     void setGain(qreal);
78     void setPitch(qreal);
79     void setRolloffFactor(qreal);
80     void setLoop(bool);
81     void setPosition(QVector3D pos);
82
83     void fade(qreal to, qreal duration, qreal from = -1.0f);
84
85     void play();
86     void pause();
87     void stop();
88     void rewind();
89
90     void setDestroyAfterStopped(bool arg)
91     {
92         if (m_destroyAfterStopped == arg)
93             return;
94
95         m_destroyAfterStopped = arg;
96         emit onDestroyAfterStoppedChanged(arg);
97     }
98
99 private:
100     unsigned int m_handle;
101     nSoundSourceRole m_role;
102
103     qreal m_gainMirror;
104
105     bool m_fading;
106     qreal m_fadeTarget;
107     qreal m_fadeDeltaPerSecond;
108
109     //GAIN CFG SUPPORT
110     static bool _m_gainsInitialized;
111     static qreal _m_musicGain;
112     static qreal _m_sfxGain;
113     static qreal _m_voiceGain;
114     static qreal _m_ambienceGain;
115
116     bool m_destroyAfterStopped;
117 };
118
119 #endif // NSOUNDSOURCE_H