/usr/share/ecere/extras/audio/alsa.ec is in ecere-extras 0.44.09.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | #if defined(__linux__)
#define uint _uint
#include <alsa/asoundlib.h>
#undef uint
import "audio"
static byte * buffer;
static snd_pcm_t *handle;
static snd_pcm_sframes_t frames;
static AudioSpec audioSpec { };
static bool paused;
static Semaphore pauseSemaphore { };
static char *device = "default";
static snd_mixer_t *mixer_handle;
static double volume, balance;
public void AudioSetBalance(double percent)
{
balance = percent;
}
public bool AudioSetVolume(VolumeControl type, double percent)
{
bool result = false;
if(type == application)
{
volume = percent;
result = true;
}
else
{
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
int pmin, pmax;
int get_vol, set_vol;
float f_multi;
snd_mixer_load(mixer_handle);
// snd_mixer_selem_id_alloca(&sid);
sid = (snd_mixer_selem_id_t *) alloca(snd_mixer_selem_id_sizeof());
memset(sid, 0, snd_mixer_selem_id_sizeof());
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, (type == pcm) ? "PCM" : "Master");
elem = snd_mixer_find_selem(mixer_handle, sid);
if(elem)
{
snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
f_multi = 1 / (float)(pmax - pmin);
set_vol = (int)(percent / f_multi + pmin + 0.5);
snd_mixer_selem_set_playback_volume(elem, 0, set_vol);
snd_mixer_selem_set_playback_volume(elem, 1, set_vol);
result = true;
}
snd_mixer_free(mixer_handle);
}
return result;
}
public bool AudioGetVolume(VolumeControl type, double * percent)
{
bool result = false;
if(type == application)
{
*percent = volume;
result = true;
}
else
{
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
long pmin, pmax;
long get_vol, set_vol;
float f_multi;
snd_mixer_load(mixer_handle);
// snd_mixer_selem_id_alloca(&sid);
sid = (snd_mixer_selem_id_t *) alloca(snd_mixer_selem_id_sizeof());
memset(sid, 0, snd_mixer_selem_id_sizeof());
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, (type == pcm) ? "PCM" : "Master");
elem = snd_mixer_find_selem(mixer_handle, sid);
if(elem)
{
snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
snd_mixer_selem_get_playback_volume(elem, 0, &set_vol);
f_multi = 1 / (float)(pmax - pmin);
*percent = f_multi * (set_vol - 0.5 - pmin);
result = true;
}
snd_mixer_free(mixer_handle);
}
return result;
}
public void OpenMixer()
{
snd_mixer_open(&mixer_handle, 0);
snd_mixer_attach(mixer_handle, "default");
snd_mixer_selem_register(mixer_handle, null, null);
}
public void CloseMixer()
{
snd_mixer_close(mixer_handle);
}
public int OpenAudio(AudioSpec wanted, AudioSpec result)
{
int err;
unsigned int i;
buffer = new byte[wanted.samples * wanted.channels * wanted.bits / 8];
memset(buffer, 0, wanted.samples * wanted.channels * wanted.bits / 8);
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
printf("Playback open error: %s\n", snd_strerror(err));
return 0;
}
if ((err = snd_pcm_set_params(handle,
(wanted.bits == 8) ? SND_PCM_FORMAT_U8 : SND_PCM_FORMAT_S16,
SND_PCM_ACCESS_RW_INTERLEAVED,
wanted.channels,
wanted.freq,
1,
500000)) < 0)
{ /* 0.5sec */
printf("Playback open error: %s\n", snd_strerror(err));
return 0;
}
soundThread.done = false;
result = wanted;
audioSpec = wanted;
volume = wanted.volume;
balance = 0.5;
return 1;
}
public void PauseAudio(int value)
{
if(!value)
{
soundThread.Create();
}
}
public void CloseAudio()
{
soundThread.done = true;
soundThread.Wait();
delete buffer;
if(handle) snd_pcm_close(handle);
}
static class SoundThread : Thread
{
bool done;
SoundThread()
{
}
~SoundThread()
{
}
uint Main()
{
while(!done)
{
if(paused)
{
pauseSemaphore.Wait();
}
else
{
int c;
short int * samples = (short int *)buffer;
double m = volume / (1 + Abs(balance * 2 - 1));
double ll = (2 - (2 * Max(balance, 0.5)))* m;
double lr = (-2 * Min(balance, 0.5) + 1) * m;
double rl = (2 * Max(balance, 0.5) - 1) * m;
double rr = (2 * Min(balance, 0.5)) * m;
// printf("Volume: %f, m : %f, Left: (%f, %f), Right: (%f, %f) \n", volume, m, ll, lr, rl, rr);
audioSpec.callback(audioSpec.userdata, buffer, audioSpec.samples * audioSpec.channels * audioSpec.bits / 8);
for(c = 0; c<audioSpec.samples; c++)
{
short sLeft = samples[0], sRight = samples[1];
samples[0] = (short)(sLeft * ll + sRight * lr);
samples[1] = (short)(sLeft * rl + sRight * rr);
samples += 2;
}
frames = snd_pcm_writei(handle, buffer, audioSpec.samples);
if(frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0)
{
printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
break;
}
if (frames > 0 && frames < audioSpec.samples)
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
}
}
return 0;
}
}
static SoundThread soundThread { };
#endif
|