Subversion Repositories Games.Carmageddon

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
 
2
#include "backend.h"
3
#include "harness/config.h"
4
#include "miniaudio/miniaudio.h"
5
#include "resource.h"
6
#include "s3_defs.h"
7
#include <assert.h>
8
#include <stdio.h>
9
#include <string.h>
10
 
11
typedef struct tS3_sample_struct_miniaudio {
12
    ma_audio_buffer_ref buffer_ref;
13
    ma_sound sound;
14
    int initialized;
15
} tS3_sample_struct_miniaudio;
16
 
17
// dethrace
18
ma_engine miniaudio_engine;
19
 
20
tAudioBackend_error_code AudioBackend_Init(void) {
21
    ma_result result;
22
 
23
    ma_engine_config engineConfig;
24
    engineConfig = ma_engine_config_init();
25
    result = ma_engine_init(&engineConfig, &miniaudio_engine);
26
    if (result != MA_SUCCESS) {
27
        printf("Failed to initialize audio engine.");
28
        return eAB_error;
29
    }
30
 
31
    ma_engine_set_volume(&miniaudio_engine, harness_game_config.volume_multiplier);
32
    return eAB_success;
33
}
34
 
35
tAudioBackend_error_code AudioBackend_InitCDA(void) {
36
    return eAB_error;
37
}
38
 
39
void AudioBackend_UnInit(void) {
40
    ma_engine_uninit(&miniaudio_engine);
41
}
42
 
43
void AudioBackend_UnInitCDA(void) {
44
}
45
 
46
void* AudioBackend_AllocateSampleTypeStruct(void) {
47
    tS3_sample_struct_miniaudio* sample_struct;
48
    sample_struct = S3MemAllocate(sizeof(tS3_sample_struct_miniaudio), kMem_S3_DOS_SOS_channel);
49
    if (sample_struct == NULL) {
50
        return 0;
51
    }
52
    memset(sample_struct, 0, sizeof(tS3_sample_struct_miniaudio));
53
    return sample_struct;
54
}
55
 
56
tAudioBackend_error_code AudioBackend_PlaySample(tS3_channel* chan) {
57
    tS3_sample_struct_miniaudio* miniaudio;
58
    tS3_sample* sample_data;
59
    ma_result result;
60
    ma_int32 flags;
61
 
62
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
63
    assert(miniaudio != NULL);
64
    sample_data = (tS3_sample*)chan->descriptor->sound_data;
65
    assert(sample_data != NULL);
66
 
67
    result = ma_audio_buffer_ref_init(ma_format_u8, sample_data->channels, sample_data->dataptr, sample_data->size / sample_data->channels, &miniaudio->buffer_ref);
68
    miniaudio->buffer_ref.sampleRate = sample_data->rate;
69
    if (result != MA_SUCCESS) {
70
        return eAB_error;
71
    }
72
 
73
    flags = MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION;
74
    result = ma_sound_init_from_data_source(&miniaudio_engine, &miniaudio->buffer_ref, flags, NULL, &miniaudio->sound);
75
    if (result != MA_SUCCESS) {
76
        return eAB_error;
77
    }
78
    miniaudio->initialized = 1;
79
 
80
    ma_sound_set_looping(&miniaudio->sound, chan->repetitions == 0);
81
    ma_sound_start(&miniaudio->sound);
82
    return eAB_success;
83
}
84
 
85
int AudioBackend_SoundIsPlaying(tS3_channel* chan) {
86
    tS3_sample_struct_miniaudio* miniaudio;
87
 
88
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
89
    assert(miniaudio != NULL);
90
 
91
    if (ma_sound_is_playing(&miniaudio->sound)) {
92
        return 1;
93
    }
94
    return 0;
95
}
96
 
97
tAudioBackend_error_code AudioBackend_SetVolume(tS3_channel* chan, int volume_db) {
98
    tS3_sample_struct_miniaudio* miniaudio;
99
    float linear_volume;
100
 
101
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
102
    assert(miniaudio != NULL);
103
 
104
    // convert from directsound -10000 - 0 decibel volume scale
105
    linear_volume = ma_volume_db_to_linear(volume_db / 100.0f);
106
    ma_sound_set_volume(&miniaudio->sound, linear_volume);
107
    return eAB_success;
108
}
109
 
110
tAudioBackend_error_code AudioBackend_SetPan(tS3_channel* chan, int pan) {
111
    tS3_sample_struct_miniaudio* miniaudio;
112
 
113
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
114
    assert(miniaudio != NULL);
115
 
116
    // convert from directsound -10000 - 10000 pan scale
117
    ma_sound_set_pan(&miniaudio->sound, pan / 10000.0f);
118
    return eAB_success;
119
}
120
 
121
tAudioBackend_error_code AudioBackend_SetFrequency(tS3_channel* chan, int rate) {
122
    tS3_sample_struct_miniaudio* miniaudio;
123
    tS3_sample* sample_data;
124
 
125
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
126
    assert(miniaudio != NULL);
127
 
128
    sample_data = (tS3_sample*)chan->descriptor->sound_data;
129
 
130
    // convert from directsound frequency to linear pitch scale
131
    ma_sound_set_pitch(&miniaudio->sound, (rate / (float)sample_data->rate));
132
    return eAB_success;
133
}
134
 
135
tAudioBackend_error_code AudioBackend_StopSample(tS3_channel* chan) {
136
    tS3_sample_struct_miniaudio* miniaudio;
137
 
138
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
139
    assert(miniaudio != NULL);
140
 
141
    if (miniaudio->initialized) {
142
        ma_sound_stop(&miniaudio->sound);
143
        ma_sound_uninit(&miniaudio->sound);
144
        ma_audio_buffer_ref_uninit(&miniaudio->buffer_ref);
145
        miniaudio->initialized = 0;
146
    }
147
    return eAB_success;
148
}