summaryrefslogtreecommitdiff
path: root/src/audio/sound.c
blob: 58a78bad13d68527d615c34b447de7b3fca76635 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "storage.h"
#include "interface.h"
#include "audio.h"
#include "sound.h"

static list_t *listStorage;
static bool_t isSoundInit = FALSE;
static bool_t var_sound_is_active = TRUE;

/*
 * Return status of sound
 */
bool_t sound_is_inicialized()
{
	return isSoundInit;
}

/*
 * Initialize sound
 */
void sound_init()
{
	if (audio_is_inicialized() == FALSE) {
		isSoundInit = FALSE;
		return;
	}

	DEBUG_MSG(_("[Debug] Initializing sound\n"));

	listStorage = storage_new();
	isSoundInit = TRUE;
	var_sound_is_active = TRUE;

	if (isParamFlag("--no-sound")) {
		sound_set_active(FALSE);
	} else if (isParamFlag("--sound")) {
		sound_set_active(TRUE);
	}
}

/*
 * Load sound from file (Null/mixer)
 */
static Mix_Chunk *loadMixSound(char *file)
{
	Mix_Chunk *new;
	char str[STR_PATH_SIZE];

	DEBUG_MSG(_("[Debug] Loading sound [%s]\n"), file);

	sprintf(str, PATH_SOUND "%s", file);
	accessExistFile(str);
	new = Mix_LoadWAV(str);

	if (new == NULL) {
		fprintf(stderr, _("[Error] Unable to load sound [%s]: %s\n"), str, Mix_GetError());
		return NULL;
	}

	return new;
}

/*
 * Play sound with mixer
 */
static void playMixSound(Mix_Chunk *p)
{
	if (Mix_PlayChannel(-1, p, 0) == -1) {
		fprintf(stderr, _("[Error] Unable to play sound: %s\n"), Mix_GetError());
		return;
	}
}

/*
 * Destroy mixer chunk
 */
static void destroySound(void *p)
{
	Mix_FreeChunk((Mix_Chunk *) p);
}

/*
 * Add a sound to list
 */
void sound_add(char *file, char *name, char *group)
{
	Mix_Chunk *new;

	if (isSoundInit == FALSE) {
		return;
	}

	assert(file != NULL);
	assert(name != NULL);
	assert(group != NULL);

	new = loadMixSound(file);
	storage_add(listStorage, group, name, new);
}

/*
 * Start playing a sound from a file from the list
 */
void sound_play(char *name, char *group)
{
	if (isSoundInit == FALSE || var_sound_is_active == FALSE) {
		return;
	}

	playMixSound(storage_get(listStorage, group, name));
}

/*
 * Set sound status to active/inactive (true/false)
 */
void sound_set_active(bool_t n)
{
	var_sound_is_active = n;
}

/*
 * Return status of sound
 */
bool_t sound_is_active()
{
	return var_sound_is_active;
}

/*
 * Shutdown sound
 */
void sound_quit()
{
	if (isSoundInit == FALSE) {
		return;
	}

	DEBUG_MSG(_("[Debug] Shutting down sound\n"));

	storage_destroy(listStorage, destroySound);
	isSoundInit = FALSE;
}