revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / AHI / Drivers / Alsa / alsa-bridge / alsa.c
blob2dbcb7f8c168fcc502b8d40668c6270bef13b407
1 /*
2 Copyright © 2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "alsa_hostlib.h"
7 #include "alsa.h"
9 #define CARDNAME "default"
10 #define VOLUMENAME "Master"
12 BOOL ALSA_Init()
14 return ALSA_HostLib_Init();
17 VOID ALSA_Cleanup()
19 ALSA_HostLib_Cleanup();
22 VOID ALSA_MixerInit(APTR * handle, APTR * elem, LONG * min, LONG * max)
24 snd_mixer_t * _handle;
25 snd_mixer_selem_id_t * _sid;
26 snd_mixer_elem_t * _elem;
28 *handle = NULL;
29 *elem = NULL;
31 ALSACALL(snd_mixer_open,&_handle, 0);
32 ALSACALL(snd_mixer_attach,_handle, CARDNAME);
33 ALSACALL(snd_mixer_selem_register,_handle, NULL, NULL);
34 ALSACALL(snd_mixer_load,_handle);
36 ALSACALL(snd_mixer_selem_id_malloc,&_sid);
37 ALSACALL(snd_mixer_selem_id_set_index,_sid, 0);
38 ALSACALL(snd_mixer_selem_id_set_name,_sid, VOLUMENAME);
39 _elem = ALSACALL(snd_mixer_find_selem,_handle, _sid);
41 if (_elem != NULL)
43 long a,b;
44 ALSACALL(snd_mixer_selem_get_playback_volume_range,_elem, &a, &b);
46 *handle = _handle;
47 *elem = _elem;
48 *min = (LONG)a;
49 *max = (LONG)b;
52 ALSACALL(snd_mixer_selem_id_free,_sid);
55 VOID ALSA_MixerCleanup(APTR handle)
57 ALSACALL(snd_mixer_close,handle);
60 LONG ALSA_MixerGetVolume(APTR elem)
62 long _ret = 0;
64 ALSACALL(snd_mixer_selem_get_playback_volume, elem,
65 SND_MIXER_SCHN_FRONT_LEFT, &_ret);
66 return (LONG)_ret;
69 VOID ALSA_MixerSetVolume(APTR elem, LONG volume)
71 ALSACALL(snd_mixer_selem_set_playback_volume_all, elem, volume);
74 APTR ALSA_Open()
76 snd_pcm_t * handle = NULL;
79 if (ALSACALL(snd_pcm_open, &handle, CARDNAME,
80 SND_PCM_STREAM_PLAYBACK, 0) < 0)
81 return NULL;
83 return handle;
86 VOID ALSA_DropAndClose(APTR handle)
88 if (handle)
90 ALSACALL(snd_pcm_drop, handle);
91 ALSACALL(snd_pcm_close, handle);
95 BOOL ALSA_SetHWParams(APTR handle, ULONG * rate)
97 snd_pcm_hw_params_t * hw_params;
98 LONG dir = 0; int r = 0;
100 ALSACALL(snd_pcm_hw_params_malloc, &hw_params);
101 ALSACALL(snd_pcm_hw_params_any, handle, hw_params);
102 ALSACALL(snd_pcm_hw_params_set_access, handle, hw_params,
103 SND_PCM_ACCESS_RW_INTERLEAVED);
104 ALSACALL(snd_pcm_hw_params_set_format, handle, hw_params,
105 SND_PCM_FORMAT_S16_LE);
106 ALSACALL(snd_pcm_hw_params_set_channels, handle, hw_params, 2);
107 r = ALSACALL(snd_pcm_hw_params_set_rate_near, handle, hw_params, rate, &dir);
108 ALSACALL(snd_pcm_hw_params_set_buffer_size, handle, hw_params, 2048);
110 ALSACALL(snd_pcm_hw_params, handle, hw_params);
111 ALSACALL(snd_pcm_hw_params_free, hw_params);
113 return (r >= 0);
116 LONG ALSA_Write(APTR handle, APTR buffer, ULONG size)
118 LONG rc = ALSACALL(snd_pcm_writei, handle, buffer, (snd_pcm_uframes_t)size);
120 if (rc == -EPIPE)
121 rc = ALSA_XRUN;
123 return rc;
126 VOID ALSA_Prepare(APTR handle)
128 ALSACALL(snd_pcm_prepare, handle);
131 LONG ALSA_Avail(APTR handle)
133 LONG rc = ALSACALL(snd_pcm_avail_update, handle);
134 if (rc == -EPIPE)
135 rc = ALSA_XRUN;
137 return rc;