-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamesound.h
128 lines (103 loc) · 3.4 KB
/
gamesound.h
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
#ifndef GAMESOUND_H
#define GAMESOUND_H
///functions for sounds!!!
#include <string>
#include <thread>
#include <SFML/Audio.hpp>
using namespace std;
class MyStream : public sf::SoundStream
{
public:
void load(const sf::SoundBuffer& buffer)
{
// extract the audio samples from the sound buffer to our own container
m_samples.assign(buffer.getSamples(), buffer.getSamples() + buffer.getSampleCount());
// reset the current playing position
m_currentSample = 0;
// initialize the base class
initialize(buffer.getChannelCount(), buffer.getSampleRate());
}
private:
virtual bool onGetData(Chunk& data)
{
// number of samples to stream every time the function is called;
// in a more robust implementation, it would rather be a fixed
// amount of time rather than an arbitrary number of samples
const int samplesToStream = 50000;
// set the pointer to the next audio samples to be played
data.samples = &m_samples[m_currentSample];
// have we reached the end of the sound?
if (m_currentSample + samplesToStream <= m_samples.size())
{
// end not reached: stream the samples and continue
data.sampleCount = samplesToStream;
m_currentSample += samplesToStream;
return true;
}
else
{
// end of stream reached: stream the remaining samples and stop playback
data.sampleCount = m_samples.size() - m_currentSample;
m_currentSample = m_samples.size();
return false;
}
}
virtual void onSeek(sf::Time timeOffset)
{
// compute the corresponding sample index according to the sample rate and channel count
m_currentSample = static_cast<std::size_t>(timeOffset.asSeconds() * getSampleRate() * getChannelCount());
}
std::vector<sf::Int16> m_samples;
std::size_t m_currentSample;
};
//
const int sound_size=5;
#define HERO_BACKGROUND_SOUND 0
#define HERO_CRASH_SOUND 1
#define HERO_BRAKE_SOUND 2
#define HERO_COIND_SOUND 3
#define HERO_JUMP_SOUND 4
string soundlist[sound_size]={"fondo.wav","choque.wav","freno.wav","moneda.wav","salto.wav"};
sf::SoundBuffer buffer[sound_size];
MyStream stream [sound_size];
int selected_track=0;
int hero_sound;
void play_hero_thread(){
//when the hero makes an action
stream[hero_sound].play();
//play until the sound ends :3
sf::sleep(sf::seconds(1.0f));
}
void play_hero_sound(int hero_sound_code){
hero_sound=hero_sound_code;
thread *t = new thread(play_hero_thread);
}
void play_track_thread(){
//play the tracks until the end of game
int current_track=selected_track;
while(1){
stream[current_track].play();
// let it play until it is finished
while (stream[current_track].getStatus() == MyStream::Playing &&
current_track==selected_track )
sf::sleep(sf::seconds(0.1f));
//we sleep while to mix the current and next tracks
stream[current_track].stop();
current_track=selected_track;//update the new track and loop to play
}
}
void play_track(){
thread *t = new thread(play_track_thread);
}
void set_track(int track){
selected_track=track;
}
void init_sound(){
for(int i=0; i< sound_size; ++i)
buffer[i].loadFromFile(soundlist[i]);
// initialize and play our custom stream
for(int i=0; i< sound_size; ++i)
stream[i].load(buffer[i]);
}
///end of functions for sounds :3
#endif