1 /* Copyright (c) 2003-2021 James Daniels */
2 /* Distributed under the MIT License */
3 /* license terms: see LICENSE file in root or http://opensource.org/licenses/MIT */
5 #include "AAS_Shared.h"
7 int AAS_SFX_GetNumChannels()
11 return 16 - AAS_MOD_GetNumChannels();
12 else if(AAS_volscale == 8)
13 return 8 - AAS_MOD_GetNumChannels();
15 return 4 - AAS_MOD_GetNumChannels();
21 struct AAS_Channel *AAS_SFX_GetChannel(int channel)
24 return &AAS_channels[AAS_chan_rearrange[15 - channel]];
25 else if(AAS_volscale == 8)
26 return &AAS_channels[AAS_chan_rearrange[7 - channel]];
28 return &AAS_channels[AAS_chan_rearrange[3 - channel]];
31 int AAS_SFX_GetOutput(int channel)
34 return AAS_chan_rearrange[15 - channel] >> 3;
35 else if(AAS_volscale == 8)
36 return AAS_chan_rearrange[7 - channel] >> 3;
38 return AAS_chan_rearrange[3 - channel] >> 3;
41 AAS_BOOL AAS_SFX_ChannelExists(int channel)
43 if((channel >= 0) && (channel < AAS_SFX_GetNumChannels())) {
50 int AAS_SFX_Play(int channel, int sample_volume, int sample_frequency, const AAS_s8 * sample_start,
51 const AAS_s8 * sample_end, const AAS_s8 * sample_restart)
54 if(AAS_SFX_ChannelExists(channel)) {
55 if((sample_frequency >= 1) && (sample_frequency <= 65535)) {
56 if((sample_volume >= 0) && (sample_volume <= 64)) {
57 if(sample_start && (sample_end > sample_start) &&
58 (sample_restart < sample_end)) {
59 struct AAS_Channel *ch;
61 ch = AAS_SFX_GetChannel(channel);
62 ch->active = AAS_FALSE;
63 ch->volume = (sample_volume << 8) >> AAS_volscale;
64 ch->frequency = sample_frequency;
65 ch->delta = AAS_Min(4095, ((sample_frequency * AAS_mix_scale) + 32768) >> 16);
66 ch->pos = sample_start;
69 ch->loop_length = sample_end - sample_restart;
73 ch->active = AAS_TRUE;
75 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
79 return AAS_ERROR_INVALID_SAMPLE_ADDRESS;
82 return AAS_ERROR_VOLUME_OUT_OF_RANGE;
85 return AAS_ERROR_FREQUENCY_OUT_OF_RANGE;
88 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
91 return AAS_ERROR_CALL_SET_CONFIG_FIRST;
95 AAS_BOOL AAS_SFX_IsActive(int channel)
97 if(AAS_SFX_ChannelExists(channel)) {
98 return AAS_SFX_GetChannel(channel)->active;
104 int AAS_SFX_EndLoop(int channel)
106 if(AAS_initialised) {
107 if(AAS_SFX_ChannelExists(channel)) {
108 AAS_SFX_GetChannel(channel)->loop_length = 0;
112 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
115 return AAS_ERROR_CALL_SET_CONFIG_FIRST;
119 int AAS_SFX_SetFrequency(int channel, int sample_frequency)
121 if(AAS_initialised) {
122 if(AAS_SFX_ChannelExists(channel)) {
123 if((sample_frequency >= 1) && (sample_frequency <= 65535)) {
124 struct AAS_Channel *ch;
126 ch = AAS_SFX_GetChannel(channel);
127 ch->frequency = sample_frequency;
128 ch->delta = AAS_Min(4095, ((sample_frequency * AAS_mix_scale) + 32768) >> 16);
130 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
134 return AAS_ERROR_FREQUENCY_OUT_OF_RANGE;
137 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
140 return AAS_ERROR_CALL_SET_CONFIG_FIRST;
144 int AAS_SFX_SetVolume(int channel, int sample_volume)
146 if(AAS_initialised) {
147 if(AAS_SFX_ChannelExists(channel)) {
148 if((sample_volume >= 0) && (sample_volume <= 64)) {
149 AAS_SFX_GetChannel(channel)->volume = (sample_volume << 8) >> AAS_volscale;
151 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
155 return AAS_ERROR_VOLUME_OUT_OF_RANGE;
158 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
161 return AAS_ERROR_CALL_SET_CONFIG_FIRST;
165 int AAS_SFX_Stop(int channel)
167 if(AAS_initialised) {
168 if(AAS_SFX_ChannelExists(channel)) {
169 AAS_SFX_GetChannel(channel)->active = AAS_FALSE;
171 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
175 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
178 return AAS_ERROR_CALL_SET_CONFIG_FIRST;
182 int AAS_SFX_Resume(int channel)
184 if(AAS_initialised) {
185 if(AAS_SFX_ChannelExists(channel)) {
186 struct AAS_Channel *ch;
188 ch = AAS_SFX_GetChannel(channel);
191 if(ch->loop_length) {
192 ch->active = AAS_TRUE;
194 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
198 if(ch->pos < ((ch->end - (ch->delta >> 6)) - 1)) {
199 ch->active = AAS_TRUE;
201 AAS_changed[AAS_SFX_GetOutput(channel)] = AAS_TRUE;
205 return AAS_ERROR_CHANNEL_UNRESUMEABLE;
209 return AAS_ERROR_CHANNEL_ACTIVE;
212 return AAS_ERROR_CHANNEL_NOT_AVAILABLE;
215 return AAS_ERROR_CALL_SET_CONFIG_FIRST;