-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.cpp
More file actions
99 lines (67 loc) · 1.83 KB
/
Copy pathSound.cpp
File metadata and controls
99 lines (67 loc) · 1.83 KB
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
#include <iostream>
#include "Sound.hpp"
/*
Esta classe é utilizada para organizar cada som do jogo.
Nela, é possível tocar o som ("play") e parar ("pause"), por exemplo.
*/
Sound::Sound(std::string _path) {
path = _path;
if (SDL_LoadWAV(path.c_str(), &wav_specs, &wav_buffer, &audio_len)==NULL) {
std::cout << "Could not open sound file: " << _path << std::endl;
std::cout << SDL_GetError() << std::endl;
}
audio_pos = wav_buffer;
audio_len_miss = audio_len;
}
// função utilizada como callback do SDL_OpenAudio no método "play"
void audio_callback(void *userdata, Uint8 *stream, int len) {
Sound *sound = (Sound *)userdata;
D std::cout << 100 - 100*(double)((double)sound->audio_len_miss/(double)sound->audio_len) << "%" << std::endl;
if (sound->audio_len_miss <= 0) {
D std::cout << "stopped" << std::endl;
return;
}
len = ( len > sound->audio_len_miss ? sound->audio_len_miss : len );
SDL_memset(stream, 0, len);
SDL_MixAudio(stream, sound->audio_pos, len, SDL_MIX_MAXVOLUME);
sound->audio_pos += len;
sound->audio_len_miss -= len;
}
bool Sound::play() {
if (is_open)
return true;
wav_specs.callback = audio_callback;
wav_specs.userdata = this;
int r;
if ((r=SDL_OpenAudio(&wav_specs, NULL)) < 0) {
D std::cout << "Could not play song: "<< r << SDL_GetError()<< std::endl;
return false;
}
is_open = true;
SDL_PauseAudio(0);
while (audio_len_miss > 0)
SDL_Delay(100);
D std::cout << "stopped playing audio" << std::endl;
SDL_CloseAudio();
is_open = false;
return true;
}
void Sound::stop() {
audio_len_miss = 0;
}
void Sound::restart() {
audio_len_miss = audio_len;
audio_pos = wav_buffer;
}
void Sound::free() {
SDL_FreeWAV(wav_buffer);
}
void Sound::pause() {
SDL_PauseAudio(1);
}
void Sound::resume() {
SDL_PauseAudio(0);
}
bool Sound::isOver() {
return audio_len_miss<=0;
}