-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.cpp
More file actions
93 lines (78 loc) · 2.6 KB
/
sound.cpp
File metadata and controls
93 lines (78 loc) · 2.6 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
#include "sound.hpp"
#include <fmod/fmod_errors.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
FMOD_SYSTEM *player = nullptr;
FMOD_SOUND *backgroundMusic = nullptr;
FMOD_CHANNEL *musicChannel = nullptr;
bool initializeSoundSystem() {
std::srand(std::time(nullptr)); // Initialize random seed
FMOD_RESULT result = FMOD_System_Create(&player);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
return false;
}
result = FMOD_System_Init(player, 512, FMOD_INIT_NORMAL, nullptr);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
return false;
}
return true;
}
void updateSoundSystem() {
FMOD_System_Update(player);
}
void playBackgroundMusic(const std::string& filePath) {
if (backgroundMusic) {
FMOD_Sound_Release(backgroundMusic);
}
FMOD_RESULT result = FMOD_System_CreateSound(player, filePath.c_str(), FMOD_LOOP_NORMAL | FMOD_2D, nullptr, &backgroundMusic);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
return;
}
result = FMOD_System_PlaySound(player, FMOD_CHANNEL_FREE, backgroundMusic, false, &musicChannel);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
}
}
void stopBackgroundMusic() {
if (musicChannel) {
FMOD_Channel_Stop(musicChannel);
}
if (backgroundMusic) {
FMOD_Sound_Release(backgroundMusic);
backgroundMusic = nullptr;
}
}
void playSoundEffect(const std::string& filePath) {
FMOD_SOUND *soundEffect;
FMOD_RESULT result = FMOD_System_CreateSound(player, filePath.c_str(), FMOD_DEFAULT, nullptr, &soundEffect);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
return;
}
result = FMOD_System_PlaySound(player, FMOD_CHANNEL_FREE, soundEffect, false, &musicChannel);
if (result != FMOD_OK) {
std::cerr << "FMOD error: " << FMOD_ErrorString(result) << std::endl;
}
FMOD_Sound_Release(soundEffect);
}
void setBackgroundMusicVolume(float volume) {
if (musicChannel) {
FMOD_Channel_SetVolume(musicChannel, volume);
std::cout << "Volume set to " << volume << std::endl;
} else {
std::cout << "No music channel" << std::endl;
}
}
void cleanupSoundSystem() {
if (backgroundMusic) {
FMOD_Sound_Release(backgroundMusic);
}
if (player) {
FMOD_System_Close(player);
FMOD_System_Release(player);
}
}