Dualie
Loading...
Searching...
No Matches
Music.hpp
1//
2// Created by caleb on 7/25/24.
3//
4
5#ifndef DUALIE_MUSIC_HPP
6#define DUALIE_MUSIC_HPP
7
8#include <iostream>
9#include <cstring>
10#include <opusfile.h>
11#include <3ds.h>
12#include <functional>
13#include <thread>
14
15
16#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17
18namespace dl
19{
20
24 class Music
25 {
26
27
28 ndspWaveBuf m_waveBufs[3];
29 int16_t* m_audioBuffer = nullptr;
30
31 OggOpusFile* m_opusFile;
32
33 LightEvent m_event;
34 Thread m_threadId;
35
36 volatile bool m_quit = false; // Quit flag
37 bool m_looping = false;
38
39 bool fillBuffer(ndspWaveBuf* waveBuf_);
40 void allocateBuffers();
41 void reinitialize();
42 void audioCallback(void* const nul_);
43 void audioThread();
44 static void callbackWrapper(void* obj);
45 static void threadWrapper(void* obj);
46 std::string getOpusErrorString(int error);
47
48 public:
49 Music();
50 ~Music();
51
56 void loadFromFile(std::string path);
57
61 void play();
62
66 void restart();
67
71 void stop();
72
77 void setLooping(bool looping);
78
79 static constexpr int SAMPLE_RATE = 48000; // Opus is fixed at 48kHz
80 static constexpr int SAMPLES_PER_BUF = SAMPLE_RATE * 120 / 1000; // 120ms buffer
81 static constexpr int CHANNELS_PER_SAMPLE = 2;
82 static constexpr int THREAD_AFFINITY = -1; // Execute thread on any core
83 static constexpr int THREAD_STACK_SZ = 32 * 1024; // 32kB stack for audio thread
84 static constexpr size_t WAVEBUF_SIZE = SAMPLES_PER_BUF * CHANNELS_PER_SAMPLE * sizeof(int16_t);
85 };
86
87}
88#endif //DUALIE_MUSIC_HPP
A class that handles the playback of opus audio files.
Definition Music.hpp:25
void setLooping(bool looping)
Sets whether the music should loop when finished.
Definition Music.cpp:257
void stop()
Stops the currently playing song. Maintains the current position of the music.
Definition Music.cpp:67
void restart()
Restarts the song from the beginning. If playback is stopped, it plays it.
Definition Music.cpp:57
void play()
Plays the opus music file loaded.
Definition Music.cpp:34
void loadFromFile(std::string path)
loads a opus file from romfs
Definition Music.cpp:23