-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
124 lines (101 loc) · 2.28 KB
/
Player.cpp
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
#include "pch.h"
#include "Player.h"
SAP::Player::Player()
{
result = new ma_result();
engine = new ma_engine();
sound = new ma_sound();
*result = ma_engine_init(NULL, engine);
if (*result != MA_SUCCESS) {
Debug::WriteLine("MA: ER: Failed to initialize audio engine.");
engineIsValid = false;
return;
}
engineIsValid = true;
Debug::WriteLine("MA: OK: Audio engine initialized!");
}
SAP::Player::~Player()
{
ma_engine_uninit(engine);
}
void SAP::Player::loadFile(String^ path)
{
if (fileIsValid)
{
ma_sound_uninit(sound);
}
*result = ma_sound_init_from_file_w(engine, msclr::interop::marshal_as<std::wstring>(path).c_str(), MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, NULL, NULL, sound);
if (*result != MA_SUCCESS) {
Debug::WriteLine("MA: ER: Failed to load file.");
fileIsValid = false;
return;
}
Debug::WriteLine("MA: OK: File loaded!");
fileIsValid = true;
}
void SAP::Player::play()
{
*result = ma_sound_start(sound);
if (*result != MA_SUCCESS) {
Debug::WriteLine("MA: ER: Failed start.");
}
Debug::WriteLine("MA: OK: Start!");
}
void SAP::Player::pause()
{
*result = ma_sound_stop(sound);
if (*result != MA_SUCCESS) {
Debug::WriteLine("MA: ER: Failed to stop.");
}
Debug::WriteLine("MA: OK: Pause!");
}
bool SAP::Player::paused()
{
return !ma_sound_is_playing(sound);
}
bool SAP::Player::fileValid()
{
return fileIsValid;
}
bool SAP::Player::engineValid()
{
return engineIsValid;
}
uint64_t SAP::Player::getTimeInMiliseconds()
{
return ma_sound_get_time_in_milliseconds(sound);
}
uint64_t SAP::Player::getTimeInPcmFrames()
{
return ma_sound_get_time_in_pcm_frames(sound);
}
uint64_t SAP::Player::getTotalTimeInPcmFrames()
{
uint64_t res;
*result = ma_sound_get_length_in_pcm_frames(sound, &res);
if (*result != MA_SUCCESS) {
return -1;
}
return res;
}
float SAP::Player::getTotalTimeInSeconds()
{
float res;
*result = ma_sound_get_length_in_seconds(sound, &res);
if (*result != MA_SUCCESS) {
return -1.f;
}
return res;
}
void SAP::Player::setTimeInPcmFrames(uint64_t second)
{
ma_sound_seek_to_pcm_frame(sound, second);
}
void SAP::Player::setVolume(float vol)
{
*result = ma_engine_set_volume(engine, vol);
if (*result != MA_SUCCESS) {
Debug::WriteLine("MA: ER: Failed to set volume!");
}
Debug::WriteLine("MA: OK: New volume set!");
}