summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Sound.h
blob: 1b86b65b1ae56cb371b017293917a02cfc54548b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Abstract Sound Object
*/

#ifndef Sound_h
#define Sound_h

#include "Types.h"
#include "Geometry.h"

// +--------------------------------------------------------------------+

class SoundCard;
class SoundCheck;
class Camera;

// +--------------------------------------------------------------------+

class Sound
{
public:
    static const char* TYPENAME() { return "Sound"; }

    static Sound*     CreateStream(const char* filename);
    static Sound*     CreateOggStream(const char* filename);
    static Sound*     Create(DWORD flags, LPWAVEFORMATEX format);
    static Sound*     Create(DWORD flags, LPWAVEFORMATEX format, DWORD len, LPBYTE data);
    static void       SetListener(const Camera& cam, const Vec3& vel);
    static void       UseSoundCard(SoundCard* s) { creator = s; }

public:
    Sound();
    virtual ~Sound();

    int operator==(const Sound& rhs) const { return this == &rhs; }

    enum FlagEnum {   AMBIENT   = 0x0000,
        LOCALIZED = 0x0001,
        LOC_3D    = 0x0002,
        MEMORY    = 0x0000,
        STREAMED  = 0x0004,
        ONCE      = 0x0000,
        LOOP      = 0x0008,
        FREE      = 0x0000,
        LOCKED    = 0x0010,
        DOPPLER   = 0x0020,
        INTERFACE = 0x0040,
        OGGVORBIS = 0x4000,
        RESOURCE  = 0x8000   // not playable, only used to store data
    };

    enum StatusEnum { UNINITIALIZED,
        INITIALIZING,
        READY,
        PLAYING,
        DONE };

    // once per frame:
    virtual void      Update()                      { }

    // mark for collection:
    virtual void      Release();

    // data loading:
    // this method is for streamed sounds:
    virtual HRESULT   StreamFile(const char* name, DWORD offset) { return E_NOINTERFACE; }

    // this method is for memory sounds:
    virtual HRESULT   Load(DWORD bytes, BYTE* data) { return E_NOINTERFACE; }  // => Ready

    // this method is for sound resources:
    virtual Sound*    Duplicate()                   { return 0;             }  // => Ready

    // transport operations:
    virtual HRESULT   Play()                        { return E_NOINTERFACE; }  // => Playing
    virtual HRESULT   Rewind()                      { return E_NOINTERFACE; }  // => Ready
    virtual HRESULT   Pause()                       { return E_NOINTERFACE; }  // => Ready
    virtual HRESULT   Stop()                        { return E_NOINTERFACE; }  // => Done

    // accessors / mutators
    int               IsReady()               const { return status == READY;   }
    int               IsPlaying()             const { return status == PLAYING; }
    int               IsDone()                const { return status == DONE;    }
    int               LoopCount()             const { return looped;   }

    virtual DWORD     GetFlags()              const { return flags;    }
    virtual void      SetFlags(DWORD f)             { flags = f;       }
    virtual DWORD     GetStatus()             const { return status;   }

    virtual long      GetVolume()             const { return volume;   }
    virtual void      SetVolume(long v)             { volume = v;      }
    virtual long      GetPan()                const { return 0;        }
    virtual void      SetPan(long p)                {                  }

    // (only for streamed sounds)
    virtual double    GetTotalTime()          const { return 0; }
    virtual double    GetTimeRemaining()      const { return 0; }
    virtual double    GetTimeElapsed()        const { return 0; }

    // These should be relative to the listener:
    // (only used for localized sounds)
    virtual const Vec3& GetLocation()         const { return location; }
    virtual void        SetLocation(const Vec3& l)  { location = l;    }
    virtual const Vec3& GetVelocity()         const { return velocity; }
    virtual void        SetVelocity(const Vec3& v)  { velocity = v;    }

    virtual float     GetMinDistance()        const { return 0; }
    virtual void      SetMinDistance(float f)       {           }
    virtual float     GetMaxDistance()        const { return 0; }
    virtual void      SetMaxDistance(float f)       {           }

    virtual void      SetSoundCheck(SoundCheck* s)  { sound_check = s; }
    virtual void      AddToSoundCard();

    const char*       GetFilename()           const { return filename; }
    void              SetFilename(const char* s);

protected:
    DWORD             flags;
    DWORD             status;
    long              volume;  // centibels, (0 .. -10000)
    int               looped;
    Vec3              location;
    Vec3              velocity;
    SoundCheck*       sound_check;
    char              filename[64];

    static SoundCard* creator;
};

// +--------------------------------------------------------------------+

class SoundCheck
{
public:
    virtual void Update(Sound* s) { }
};

#endif  // Sound_h