summary refs log tree commit diff
path: root/src/Audio.h
blob: 1ab2ae61a08eef4af5159859e74aa97f34f31354 (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
#ifndef CC_AUDIO_H
#define CC_AUDIO_H
#include "Core.h"
/* Manages playing sound and music.
   Copyright 2014-2023 ClassiCube | Licensed under BSD-3
*/
struct IGameComponent;
extern struct IGameComponent Audio_Component; 
struct AudioContext;

#ifdef CC_BUILD_WEBAUDIO
#define DEFAULT_SOUNDS_VOLUME   0
#else
#define DEFAULT_SOUNDS_VOLUME 100
#endif

#ifdef CC_BUILD_NOMUSIC
#define DEFAULT_MUSIC_VOLUME   0
#else
#define DEFAULT_MUSIC_VOLUME 100
#endif

union AudioChunkMeta { void* ptr; cc_uintptr val; };
struct AudioChunk {
	void* data; /* the raw 16 bit integer samples */
	cc_uint32 size;
	union AudioChunkMeta meta;
};

struct AudioData {
	struct AudioChunk chunk;
	int channels;
	int sampleRate; /* frequency / sample rate */
	int volume; /* volume data played at (100 = normal volume) */
	int rate;   /* speed/pitch played at (100 = normal speed) */
};

/* Volume sounds are played at, from 0-100. */
/* NOTE: Use Audio_SetSounds, don't change this directly. */
extern int Audio_SoundsVolume;
/* Volume music is played at, from 0-100. */
/* NOTE: Use Audio_SetMusic, don't change this directly. */
extern int Audio_MusicVolume;
extern const cc_string Sounds_ZipPathMC;
extern const cc_string Sounds_ZipPathCC;

void Audio_SetMusic(int volume);
void Audio_SetSounds(int volume);
void Audio_PlayDigSound(cc_uint8 type);
void Audio_PlayStepSound(cc_uint8 type);
#define AUDIO_MAX_BUFFERS 4

cc_bool AudioBackend_Init(void);
void    AudioBackend_Tick(void);
void    AudioBackend_Free(void);

/* Initialises an audio context. */
cc_result Audio_Init(struct AudioContext* ctx, int buffers);
/* Stops any playing audio and then frees the audio context. */
void Audio_Close(struct AudioContext* ctx);
/* Sets the format of the audio data to be played. */
/* NOTE: Changing the format can be expensive, depending on the backend. */
cc_result Audio_SetFormat(struct AudioContext* ctx, int channels, int sampleRate, int playbackRate);
/* Sets the volume audio data is played at */
void Audio_SetVolume(struct AudioContext* ctx, int volume);
/* Queues the given audio chunk for playing. */
/* NOTE: You MUST ensure Audio_Poll indicates a buffer is free before calling this. */
/* NOTE: Some backends directly read from the chunk data - therefore you MUST NOT modify it */
cc_result Audio_QueueChunk(struct AudioContext* ctx, struct AudioChunk* chunk);
/* Begins playing audio. Audio_QueueChunk must have been used before this. */
cc_result Audio_Play(struct AudioContext* ctx);
/* Polls the audio context and then potentially unqueues buffer */
/* Returns the number of buffers being played or queued */
/* (e.g. if inUse is 0, no audio buffers are being played or queued) */
cc_result Audio_Poll(struct AudioContext* ctx, int* inUse);
cc_result Audio_Pause(struct AudioContext* ctx); /* Only implemented with OpenSL ES backend */

/* Outputs more detailed information about errors with audio. */
cc_bool Audio_DescribeError(cc_result res, cc_string* dst);
/* Allocates a group of chunks of data to store audio samples */
cc_result Audio_AllocChunks(cc_uint32 size, struct AudioChunk* chunks, int numChunks);
/* Frees a previously allocated group of chunks of data */
void Audio_FreeChunks(struct AudioChunk* chunks, int numChunks);

extern struct AudioContext music_ctx;
void Audio_Warn(cc_result res, const char* action);

cc_result AudioPool_Play(struct AudioData* data);
void AudioPool_Close(void);
#endif