File: audio.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (149 lines) | stat: -rw-r--r-- 3,636 bytes parent folder | download | duplicates (3)
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
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010,2011,2012 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library 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 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library 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 Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MLV_audio.h"

#include "warning_error.h"

#include "memory_management.h"

#include "data_structure.h"

#include "platform.h"

#if defined( OS_WINDOWS )
#	include <SDL/SDL_mixer.h>
#elif defined( OS_APPLE )
#	include <SDL_mixer/SDL_mixer.h>
#else
#	include <SDL/SDL_mixer.h>
#endif

#include <glib.h>

extern DataMLV* MLV_data;

struct _MLV_Music {
	Mix_Music* music;
};

struct _MLV_Sound {
	Mix_Chunk* sample;
};

struct _MLV_Playlist {
    GList* list_music;
};

#define MLV_AUDIO_BUFFER_SIZE 1024
#define MLV_AUDIO_STEREO 2
#define MLV_AUDIO_NUMBER_OF_MIXING_CHANEL 16

int init_audio( int buffer_size ){
	int result = Mix_OpenAudio( 
		MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MLV_AUDIO_STEREO, buffer_size
	);
	if( !result ){
		Mix_AllocateChannels(MLV_AUDIO_NUMBER_OF_MIXING_CHANEL);
	}
	return result;
}

void MLV_change_number_of_parallel_sounds( unsigned int n ){
		if( ! n ){
			fprintf( stderr, "Incorrect parameter for MLV_change_number_playing_parallel_sounds. The number of parrallel sound can't be null." );
			exit(1);
		}
		Mix_AllocateChannels( n );
}

int MLV_init_audio(){
	return init_audio( MLV_AUDIO_BUFFER_SIZE ); // If the sound are not well 
	                                            // former, increse this value. 
	                          // If the sound have some lags, the you have 
	                          // to decrease this value.
}

int MLV_change_audio_buffer_size( int buffer_size ){
	MLV_free_audio();
	return init_audio( buffer_size );
}

void MLV_free_audio(){
	Mix_HaltChannel(-1);
	Mix_HaltMusic();
	Mix_CloseAudio();
}

MLV_Music* MLV_load_music( const char* file_music ){
	MLV_Music* result = MLV_MALLOC( 1, MLV_Music );
	result->music= Mix_LoadMUS( file_music );
	if(! result->music) {
		MLV_FREE( result, MLV_Music );
		result = NULL;
	}
	return result;
}

void MLV_free_music( MLV_Music* music ){
	if( music ){
		Mix_FreeMusic( music->music );
		MLV_FREE( music, MLV_Music );
	}
}

void MLV_play_music( const MLV_Music* music, float volume, int loop ){
	Mix_VolumeMusic( volume * MIX_MAX_VOLUME );
	MLV_stop_music();
	if( loop ) Mix_PlayMusic( music->music, loop-1 );
}

void MLV_stop_music(){
	Mix_HaltMusic();
}

MLV_Sound* MLV_load_sound( const char* file_sound ){
	MLV_Sound* result = MLV_MALLOC( 1, MLV_Sound );
	result->sample = Mix_LoadWAV( file_sound );
	if(! result->sample ){
		MLV_FREE( result, MLV_Sound );
		result = NULL;
	}else{
		Mix_VolumeChunk( result->sample, MIX_MAX_VOLUME );
	}
	return result;
}

void MLV_free_sound( MLV_Sound* sound ){
	if( sound ){
		Mix_FreeChunk( sound->sample );
		MLV_FREE( sound, MLV_Sound );
	}
}

void MLV_play_sound( const MLV_Sound* sound, float volume ){
	int channel = Mix_PlayChannel( -1, sound->sample, 0 );
	Mix_Volume(channel, volume * MIX_MAX_VOLUME);
}

void MLV_stop_all_sounds(){
	Mix_HaltChannel(-1);
}