The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
audio.h
Go to the documentation of this file.
1
#pragma once
2
3
/**
4
* @file audio.h
5
* @brief the sound output core: /dev/dsp, /dev/mixer and the driver
6
* interface.
7
*
8
* a program writes PCM to /dev/dsp. the core converts it to the device
9
* format (signed 16-bit little-endian stereo, at the device rate) and
10
* keeps it in a ring buffer. the driver plays from DMA buffers split in
11
* periods; when the device has played a period, the driver interrupt
12
* calls audio_pull to fill it again. if the ring is empty, the period is
13
* filled with silence and counted as an underrun (/proc/audio).
14
*
15
* several programs can open /dev/dsp at once (up to 8): each has its own
16
* ring, and the core adds them together for the device.
17
*/
18
19
#include "
common/stdint.h
"
20
21
typedef
struct
audio_device
audio_device_t
;
22
23
typedef
struct
24
{
25
/**
26
* start playing. the driver fills all its periods with audio_pull
27
* first. called in process context.
28
* @return false if the device cannot start.
29
*/
30
bool
(*
start
)(
audio_device_t
*dev);
31
32
/**
33
* stop playing. called in process context.
34
*/
35
void (*
stop
)(
audio_device_t
*dev);
36
37
/**
38
* use a sample rate, or the nearest one the device has. called only
39
* while the device is stopped.
40
* @param rate the wanted rate in Hz.
41
* @return the rate in use.
42
*/
43
uint32_t
(*
set_rate
)(
audio_device_t
*dev,
uint32_t
rate
);
44
45
/**
46
* set the output volume (optional).
47
* @param left the left volume, 0 to 100.
48
* @param right the right volume, 0 to 100.
49
*/
50
void (*
set_volume
)(
audio_device_t
*dev,
uint32_t
left,
uint32_t
right);
51
}
audio_ops_t
;
52
53
struct
audio_device
54
{
55
const
char
*
name
;
// the driver ("ac97", "hda")
56
const
audio_ops_t
*
ops
;
57
void
*
driver_data
;
58
uint32_t
rate
;
// the rate in use (set by the driver at register)
59
uint32_t
period_bytes
;
// bytes per DMA period
60
uint32_t
periods
;
// DMA periods
61
uint32_t
codec_id
;
// the codec's vendor and device id (/proc/audio)
62
const
char
*
detail
;
// a short note about the hardware (/proc/audio)
63
};
64
65
/**
66
* register the sound device (one, the first one wins) and make /dev/dsp
67
* and /dev/mixer.
68
* @return false if a device is already registered.
69
*/
70
bool
audio_register
(
audio_device_t
*dev);
71
72
/**
73
* fill a DMA period from the ring buffer, with silence after the data.
74
* call it from the driver interrupt (or from start).
75
* @param dst the period.
76
* @param bytes its size (a multiple of 4).
77
* @return the bytes of real sound in it.
78
*/
79
uint32_t
audio_pull
(
audio_device_t
*dev,
void
*dst,
uint32_t
bytes);
80
81
/**
82
* the state for /proc/audio.
83
*/
84
typedef
struct
85
{
86
bool
present
;
87
const
char
*
name
;
88
uint32_t
codec_id
;
89
const
char
*
detail
;
90
uint32_t
rate
;
91
uint32_t
open
;
// programs that have /dev/dsp open
92
bool
running
;
93
uint32_t
queued
;
// bytes in the ring
94
uint32_t
underruns
;
// periods filled with silence while a program played
95
uint64_t
played
;
// bytes given to the device
96
uint32_t
volume
;
// left | right << 8
97
}
audio_status_t
;
98
99
void
audio_status
(
audio_status_t
*status);
100
101
/**
102
* add 16-bit little-endian samples to sums (the mixing of the streams).
103
*/
104
void
audio_mix_add
(
int32_t
*sums,
const
uint8_t
*frames,
uint32_t
samples);
105
106
/**
107
* write sums as 16-bit little-endian samples, clipped to the 16-bit range.
108
*/
109
void
audio_mix_out
(
uint8_t
*out,
const
int32_t
*sums,
uint32_t
samples);
audio_register
bool audio_register(audio_device_t *dev)
register the sound device (one, the first one wins) and make /dev/dsp and /dev/mixer.
Definition
audio.c:638
audio_status
void audio_status(audio_status_t *status)
Definition
audio.c:665
audio_pull
uint32_t audio_pull(audio_device_t *dev, void *dst, uint32_t bytes)
fill a DMA period from the ring buffer, with silence after the data.
Definition
audio.c:266
audio_mix_out
void audio_mix_out(uint8_t *out, const int32_t *sums, uint32_t samples)
write sums as 16-bit little-endian samples, clipped to the 16-bit range.
Definition
audio.c:241
audio_mix_add
void audio_mix_add(int32_t *sums, const uint8_t *frames, uint32_t samples)
add 16-bit little-endian samples to sums (the mixing of the streams).
Definition
audio.c:233
audio_device_t
struct audio_device audio_device_t
Definition
audio.h:21
stdint.h
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
int32_t
int int32_t
Definition
stdint.h:14
bool
#define bool
Definition
stdbool.h:8
audio_device
Definition
audio.h:54
audio_device::periods
uint32_t periods
Definition
audio.h:60
audio_device::driver_data
void * driver_data
Definition
audio.h:57
audio_device::period_bytes
uint32_t period_bytes
Definition
audio.h:59
audio_device::rate
uint32_t rate
Definition
audio.h:58
audio_device::detail
const char * detail
Definition
audio.h:62
audio_device::codec_id
uint32_t codec_id
Definition
audio.h:61
audio_device::ops
const audio_ops_t * ops
Definition
audio.h:56
audio_device::name
const char * name
Definition
audio.h:55
audio_ops_t
Definition
audio.h:24
audio_ops_t::set_rate
uint32_t(*) set_rate(audio_device_t *dev, uint32_t rate)
use a sample rate, or the nearest one the device has.
Definition
audio.h:43
audio_ops_t::start
bool(*) start(audio_device_t *dev)
start playing.
Definition
audio.h:30
audio_ops_t::set_volume
void(*) set_volume(audio_device_t *dev, uint32_t left, uint32_t right)
set the output volume (optional).
Definition
audio.h:50
audio_ops_t::stop
void(*) stop(audio_device_t *dev)
stop playing.
Definition
audio.h:35
audio_status_t
the state for /proc/audio.
Definition
audio.h:85
audio_status_t::volume
uint32_t volume
Definition
audio.h:96
audio_status_t::rate
uint32_t rate
Definition
audio.h:90
audio_status_t::running
bool running
Definition
audio.h:92
audio_status_t::played
uint64_t played
Definition
audio.h:95
audio_status_t::present
bool present
Definition
audio.h:86
audio_status_t::codec_id
uint32_t codec_id
Definition
audio.h:88
audio_status_t::queued
uint32_t queued
Definition
audio.h:93
audio_status_t::detail
const char * detail
Definition
audio.h:89
audio_status_t::name
const char * name
Definition
audio.h:87
audio_status_t::underruns
uint32_t underruns
Definition
audio.h:94
audio_status_t::open
uint32_t open
Definition
audio.h:91
src
kernel
audio
audio.h
Generated by
1.18.0