summaryrefslogtreecommitdiff
path: root/libs/revolution/src/homebuttonLib/nw4hbm/snd/SoundInstanceManager.h
blob: 9649d6920b1dd3cb52bcc13aac63fc24846d70c7 (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
#ifndef NW4HBM_SND_SOUND_INSTANCE_MANAGER_H
#define NW4HBM_SND_SOUND_INSTANCE_MANAGER_H

#include <new>

#include <revolution/types.h>

#include "InstancePool.h"

#include "../ut/Lock.h"
#include "../ut/inlines.h"

#include <revolution/os.h>

#include "../db/assert.h"

namespace nw4hbm {
    namespace snd {
        namespace detail {

            template <typename T>
            class SoundInstanceManager {
            public:
                u32 Create(void* buffer, u32 size) {
                    NW4HBM_ASSERT_CHECK_NULL(59, buffer);
                    return mPool.Create(buffer, size);
                }

                void Destroy(void* buffer, u32 size) {
                    NW4HBM_ASSERT_CHECK_NULL(76, buffer);
                    mPool.Destroy(buffer, size);
                }

                T* Alloc(int priority) {
                    NW4R_ASSERT_MINMAXLT(92, priority, 0, 127);
                    ut::AutoInterruptLock lock;
                    T* sound;

                    void* ptr = mPool.Alloc();

                    if (ptr != NULL) {
                        sound = new (ptr) T(this);
                    } else {
                        if (mPriorityList.IsEmpty()) {
                            return NULL;
                        }
                        sound = &mPriorityList.GetFront();

                        if (sound == NULL) {
                            return NULL;
                        }

                        if (priority < sound->CalcCurrentPlayerPriority()) {
                            return NULL;
                        }
                        sound->Stop();

                        ptr = mPool.Alloc();
                        NW4HBM_ASSERT_CHECK_NULL(114, ptr);
                        sound = new (ptr) T(this);
                    }

                    InsertPriorityList(sound, priority);
                    return sound;
                }

                void Free(T* sound) {
                    NW4HBM_ASSERT_CHECK_NULL(134, sound);
                    ut::AutoInterruptLock lock;

                    if (mPriorityList.IsEmpty()) {
                        return;
                    }

                    RemovePriorityList(sound);
                    sound->~T();
                    mPool.Free(sound);
                }

                u32 GetActiveCount() const { return mPriorityList.GetSize(); }

                u32 GetFreeCount() const { return mPool.Count(); }

                T* GetLowestPrioritySound() {
                    if (mPriorityList.IsEmpty()) {
                        return NULL;
                    }

                    return static_cast<T*>(&mPriorityList.GetFront());
                }

                void InsertPriorityList(T* sound, int priority) {
                    TPrioList::Iterator it = mPriorityList.GetBeginIter();

                    for (; it != mPriorityList.GetEndIter(); it++) {
                        if (priority < it->CalcCurrentPlayerPriority()) {
                            break;
                        }
                    }

                    mPriorityList.Insert(it, sound);
                }

                void RemovePriorityList(T* sound) { mPriorityList.Erase(sound); }

                void SortPriorityList() {
                    TPrioList listsByPrio[T::PRIORITY_MAX + 1];

                    while (!mPriorityList.IsEmpty()) {
                        T& rSound = mPriorityList.GetFront();
                        mPriorityList.PopFront();
                        listsByPrio[rSound.CalcCurrentPlayerPriority()].PushBack(&rSound);
                    }

                    for (int i = 0; i < T::PRIORITY_MAX + 1; i++) {
                        while (!listsByPrio[i].IsEmpty()) {
                            T& rSound = listsByPrio[i].GetFront();
                            listsByPrio[i].PopFront();
                            mPriorityList.PushBack(&rSound);
                        }
                    }
                }

                void UpdatePriority(T* sound, int priority) {
                    RemovePriorityList(sound);
                    InsertPriorityList(sound, priority);
                }

            private:
                typedef ut::LinkList<T, offsetof(T, mPriorityLink)> TPrioList;

            private:
                /* 0x00 */ MemoryPool<T> mPool;
                /* 0x04 */ TPrioList mPriorityList;
            };

        }  // namespace detail
    }  // namespace snd
}  // namespace nw4hbm

#endif