Subversion Repositories Games.Carmageddon

Rev

Rev 1 | Details | Compare with Previous | 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;
21 pmbaty 14
    int init_volume;
15
    int init_pan;
16
    int init_new_rate;
1 pmbaty 17
    int initialized;
18
} tS3_sample_struct_miniaudio;
19
 
20
// dethrace
21
ma_engine miniaudio_engine;
22
 
23
tAudioBackend_error_code AudioBackend_Init(void) {
24
    ma_result result;
25
 
26
    ma_engine_config engineConfig;
27
    engineConfig = ma_engine_config_init();
28
    result = ma_engine_init(&engineConfig, &miniaudio_engine);
29
    if (result != MA_SUCCESS) {
30
        printf("Failed to initialize audio engine.");
31
        return eAB_error;
32
    }
33
 
34
    ma_engine_set_volume(&miniaudio_engine, harness_game_config.volume_multiplier);
35
    return eAB_success;
36
}
37
 
38
tAudioBackend_error_code AudioBackend_InitCDA(void) {
39
    return eAB_error;
40
}
41
 
42
void AudioBackend_UnInit(void) {
43
    ma_engine_uninit(&miniaudio_engine);
44
}
45
 
46
void AudioBackend_UnInitCDA(void) {
47
}
48
 
49
void* AudioBackend_AllocateSampleTypeStruct(void) {
50
    tS3_sample_struct_miniaudio* sample_struct;
51
    sample_struct = S3MemAllocate(sizeof(tS3_sample_struct_miniaudio), kMem_S3_DOS_SOS_channel);
52
    if (sample_struct == NULL) {
53
        return 0;
54
    }
55
    memset(sample_struct, 0, sizeof(tS3_sample_struct_miniaudio));
56
    return sample_struct;
57
}
58
 
59
tAudioBackend_error_code AudioBackend_PlaySample(tS3_channel* chan) {
60
    tS3_sample_struct_miniaudio* miniaudio;
61
    tS3_sample* sample_data;
62
    ma_result result;
63
    ma_int32 flags;
64
 
65
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
66
    assert(miniaudio != NULL);
67
    sample_data = (tS3_sample*)chan->descriptor->sound_data;
68
    assert(sample_data != NULL);
69
 
70
    result = ma_audio_buffer_ref_init(ma_format_u8, sample_data->channels, sample_data->dataptr, sample_data->size / sample_data->channels, &miniaudio->buffer_ref);
71
    miniaudio->buffer_ref.sampleRate = sample_data->rate;
72
    if (result != MA_SUCCESS) {
73
        return eAB_error;
74
    }
75
 
76
    flags = MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION;
77
    result = ma_sound_init_from_data_source(&miniaudio_engine, &miniaudio->buffer_ref, flags, NULL, &miniaudio->sound);
78
    if (result != MA_SUCCESS) {
79
        return eAB_error;
80
    }
81
    miniaudio->initialized = 1;
82
 
21 pmbaty 83
    if (miniaudio->init_volume > 0)
84
    {
85
       AudioBackend_SetVolume (chan, miniaudio->init_volume);
86
       AudioBackend_SetPan (chan, miniaudio->init_pan);
87
       AudioBackend_SetFrequency (chan, miniaudio->init_new_rate);
88
    }
89
 
1 pmbaty 90
    ma_sound_set_looping(&miniaudio->sound, chan->repetitions == 0);
91
    ma_sound_start(&miniaudio->sound);
92
    return eAB_success;
93
}
94
 
95
int AudioBackend_SoundIsPlaying(tS3_channel* chan) {
96
    tS3_sample_struct_miniaudio* miniaudio;
97
 
98
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
99
    assert(miniaudio != NULL);
100
 
101
    if (ma_sound_is_playing(&miniaudio->sound)) {
102
        return 1;
103
    }
104
    return 0;
105
}
106
 
21 pmbaty 107
tAudioBackend_error_code AudioBackend_SetVolume(tS3_channel* chan, int volume) {
1 pmbaty 108
    tS3_sample_struct_miniaudio* miniaudio;
109
    float linear_volume;
110
 
111
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
112
    assert(miniaudio != NULL);
113
 
21 pmbaty 114
    if (!miniaudio->initialized)
115
    {
116
       miniaudio->init_volume = volume;
117
       return eAB_success;
118
    }
119
 
120
    linear_volume = volume / 510.0f;
1 pmbaty 121
    ma_sound_set_volume(&miniaudio->sound, linear_volume);
122
    return eAB_success;
123
}
124
 
125
tAudioBackend_error_code AudioBackend_SetPan(tS3_channel* chan, int pan) {
126
    tS3_sample_struct_miniaudio* miniaudio;
127
 
128
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
129
    assert(miniaudio != NULL);
130
 
21 pmbaty 131
    if (!miniaudio->initialized)
132
    {
133
       miniaudio->init_pan = pan;
134
       return eAB_success;
135
    }
136
 
1 pmbaty 137
    // convert from directsound -10000 - 10000 pan scale
138
    ma_sound_set_pan(&miniaudio->sound, pan / 10000.0f);
139
    return eAB_success;
140
}
141
 
142
tAudioBackend_error_code AudioBackend_SetFrequency(tS3_channel* chan, int rate) {
143
    tS3_sample_struct_miniaudio* miniaudio;
144
    tS3_sample* sample_data;
145
 
146
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
147
    assert(miniaudio != NULL);
148
 
21 pmbaty 149
    if (!miniaudio->initialized)
150
    {
151
       miniaudio->init_new_rate = rate;
152
       return eAB_success;
153
    }
154
 
1 pmbaty 155
    sample_data = (tS3_sample*)chan->descriptor->sound_data;
156
 
157
    // convert from directsound frequency to linear pitch scale
158
    ma_sound_set_pitch(&miniaudio->sound, (rate / (float)sample_data->rate));
159
    return eAB_success;
160
}
161
 
162
tAudioBackend_error_code AudioBackend_StopSample(tS3_channel* chan) {
163
    tS3_sample_struct_miniaudio* miniaudio;
164
 
165
    miniaudio = (tS3_sample_struct_miniaudio*)chan->type_struct_sample;
166
    assert(miniaudio != NULL);
167
 
168
    if (miniaudio->initialized) {
169
        ma_sound_stop(&miniaudio->sound);
170
        ma_sound_uninit(&miniaudio->sound);
171
        ma_audio_buffer_ref_uninit(&miniaudio->buffer_ref);
172
        miniaudio->initialized = 0;
173
    }
174
    return eAB_success;
175
}