File: sound.c

package info (click to toggle)
gweled 0.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,468 kB
  • ctags: 516
  • sloc: ansic: 4,170; sh: 2,082; makefile: 100
file content (168 lines) | stat: -rw-r--r-- 3,944 bytes parent folder | download | duplicates (4)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Gweled
 *
 * Copyright (C) 2003-2005 Sebastien Delestaing <sebastien.delestaing@wanadoo.fr>
 * Copyright (C) 2010 Daniele Napolitano <dnax88@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <mikmod.h>
#include <pthread.h>
#include <glib.h>

#include "sound.h"
#include "board_engine.h"

static pthread_t thread;

static MODULE *module;
static SAMPLE *swap_sfx = NULL;
static SAMPLE *click_sfx = NULL;

static gboolean is_playing;
static gboolean sound_available;

void sound_thread(void *ptr)
{
	while (1) {
	    g_usleep(10000);
		MikMod_Update();
    }
}

void sound_init()
{
    /* register all the drivers */
    MikMod_RegisterDriver(&drv_esd);
    MikMod_RegisterDriver(&drv_alsa);
    MikMod_RegisterDriver(&drv_oss);
    MikMod_RegisterDriver(&drv_nos);

    /* register only the s3m module loader */
    MikMod_RegisterLoader(&load_s3m);

    /* initialize the library */
    if (MikMod_Init("")) {
        g_printerr("Could not initialize sound, reason: %s\n", MikMod_strerror(MikMod_errno));
        MikMod_Exit();
        sound_available = FALSE;
    } else {
        sound_available = TRUE;
        MikMod_EnableOutput();
        pthread_create(&thread, NULL, (void *)&sound_thread, NULL);
        g_print("Audio driver choosen: %s\n", md_driver->Name);
    }

    is_playing = FALSE;
}


void sound_music_play()
{
	if (!is_playing && sound_available) {
	    // load module
	    module = Player_Load(DATADIR "/sounds/gweled/autonom.s3m", 64, 0);
    	if (module) {
    	    Player_Start(module);
			Player_SetVolume(64);
			is_playing = TRUE;

    	} else
    	    g_printerr("Could not load module, reason: %s\n", MikMod_strerror(MikMod_errno));
	}
}

void sound_music_stop()
{
	if (is_playing && sound_available) {
    	if (module)
		{
	    	Player_Stop();
	    	Player_Free(module);
	    	is_playing = FALSE;
		}
	}
}

// load sound fx
void sound_load_samples()
{
    if (sound_available == FALSE)
        return;

    if (!swap_sfx)
        swap_sfx = Sample_Load(DATADIR "/sounds/gweled/swap.wav");
    if (!swap_sfx)
        g_warning("Could not load swap.wav, reason: %s", MikMod_strerror(MikMod_errno));

    if (!click_sfx)
        click_sfx = Sample_Load(DATADIR "/sounds/gweled/click.wav");
    if (!click_sfx)
        g_warning("Could not load click.wav, reason: %s", MikMod_strerror(MikMod_errno));

    MikMod_SetNumVoices(-1, 2);
}

void sound_unload_samples()
{
    if (swap_sfx) {
		Sample_Free(swap_sfx);
		swap_sfx = NULL;
	}
	if (click_sfx) {
		Sample_Free(click_sfx);
		click_sfx = NULL;
	}
}

// play sound fx
void sound_play_sample(gweled_sound_samples sample)
{
    if (sound_available == FALSE)
        return;

    switch (sample) {
        case CLICK_EVENT:
            if(click_sfx)
                Sample_Play(click_sfx, 0, 0);
            break;
        case SWAP_EVENT:
            if(swap_sfx)
                Sample_Play(swap_sfx, 0, 0);
            break;
    }
}

void sound_destroy()
{
    if (sound_available) {
        sound_music_stop();

        pthread_cancel(thread);
        pthread_join(thread, NULL);

        sound_unload_samples();

        MikMod_DisableOutput();
	    MikMod_Exit();

	    sound_available = FALSE;
	}
}

gboolean sound_get_enabled()
{
    return sound_available;
}