summaryrefslogtreecommitdiff
path: root/src/audio/sound.c
blob: 7a0bfc29b747c927fab9bb123ea4655cf66f63e3 (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
149
150
151
152

#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_isSoundActive = TRUE;
/*
 * Return state of sound
 */
bool_t
isSoundInicialized()
{
   return isSoundInit;
}

/*
 * Audio initialization
 */
void
initSound()
{
   if (isAudioInicialized() == FALSE) {
      isSoundInit = FALSE;
      return;
   }

   listStorage = newStorage();
   isSoundInit = TRUE;
   var_isSoundActive = TRUE;
   if (isParamFlag("--nosound"))
      setSoundActive(FALSE);
   if (isParamFlag("--sound"))
      setSoundActive(TRUE);
#ifdef DEBUG
   printf(_("Initializing sound...\n"));
#endif
}

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

#ifdef DEBUG
   printf(_("Loading sound file: %s\n"), file);
#endif
   sprintf(str, PATH_SOUND "%s", file);
   accessExistFile(str);
   new = Mix_LoadWAV(str);
   if (new == NULL) {
      fprintf(stderr, _("Unable to load sound file: %s with error: %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, _("Unable to playback sound with error: %s\n"),
              Mix_GetError());
      return;
   }
}

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

/*
 * Add sound to list
 */
void
addSound(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);
   addItemToStorage(listStorage, group, name, new);
}

/*
 * Start playback file from list
 */
void
playSound(char *name, char *group)
{
   if (isSoundInit == FALSE || var_isSoundActive == FALSE)
      return;
   playMixSound(getItemFromStorage(listStorage, group, name));
}

/*
 * Set if sound is active True/False
 */
void
setSoundActive(bool_t n)
{
   var_isSoundActive = n;
}

/*
 * Return state of sound
 */
bool_t
isSoundActive()
{
   return var_isSoundActive;
}

/*
 * Quit all sound stuff
 */
void
quitSound()
{
   if (isSoundInit == FALSE)
      return;
   destroyStorage(listStorage, destroySound);
   isSoundInit = FALSE;
#ifdef DEBUG
   printf(_("Quitting sound...\n"));
#endif
}