aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/audio/sdl/source.h
blob: 682ff5066d6fe1613e918b6da0afeb2651af7d6e (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
#ifndef __JIN_SOURCE_SDL_H
#define __JIN_SOURCE_SDL_H

#include <vector>
#include <queue>
#include <stack>

#include "3rdparty/wav/wav.h"
#define STB_VORBIS_HEADER_ONLY
#include "3rdparty/stb/stb_vorbis.c"
#undef STB_VORBIS_HEADER_ONLY

#include "../source.h"

namespace jin
{
namespace audio
{

    class SDLSource : public Source
    {

    public: 

        SDLSource() {} 
        ~SDLSource() {}

        void generateSamples(Uint8* streamIn, int len);

        /* ISource interface */
        void play() override;
        void stop() override;
        void pause() override;
        void resume() override;
        void rewind() override;
        void isStopped() const override;
        void isPaused() const override;
        void isFinished() const override;
        void setPitch(float pitch) override;
        void setVolume(float volume) override;
        bool setLoop(bool loop) override;
        void setRate(float rate) override;

        /* process function */
        static void processCommand();
        static void processSource(); 
        static void removeSource(SDLSource* source);

    private:

        struct Command
        {
            typedef enum Action
            {
                Nothing = 0,
                Play,
                Stop,
                Pause,
                Resume,
                Rewind,
                SetVolume,
                SetLoop,
                SetRate,
            };
            Action action;
            union {
                int _integer;
                float _float; 
                bool _boolean;
                const char* _string;
            };
            SDLSource* source;
        };

        static std::queue<Command*> commands;
        static std::stack<Command*> commandsPool;
        static Command* getCommand();
        static void collectCommand(Command* cmd);

        static std::vector<SDLSource*> sources; 
        
        void* data;
        void* pos; 
        int length;
        int pitch; 
        bool paused; 
        bool playing;
        bool stopped; 

    };

}
}

#endif