The manual
ginnOS
The reference: libc, the libraries and the kernel
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
22
23typedef 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 */
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 */
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);
52
54{
55 const char *name; // the driver ("ac97", "hda")
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 */
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 */
79uint32_t audio_pull(audio_device_t *dev, void *dst, uint32_t bytes);
80
81/**
82 * the state for /proc/audio.
83 */
84typedef struct
85{
86 bool present;
87 const char *name;
89 const char *detail;
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
98
99void audio_status(audio_status_t *status);
100
101/**
102 * add 16-bit little-endian samples to sums (the mixing of the streams).
103 */
104void 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 */
109void audio_mix_out(uint8_t *out, const int32_t *sums, uint32_t samples);
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
void audio_status(audio_status_t *status)
Definition audio.c:665
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
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
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
struct audio_device audio_device_t
Definition audio.h:21
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
#define bool
Definition stdbool.h:8
Definition audio.h:54
uint32_t periods
Definition audio.h:60
void * driver_data
Definition audio.h:57
uint32_t period_bytes
Definition audio.h:59
uint32_t rate
Definition audio.h:58
const char * detail
Definition audio.h:62
uint32_t codec_id
Definition audio.h:61
const audio_ops_t * ops
Definition audio.h:56
const char * name
Definition audio.h:55
Definition audio.h:24
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
bool(*) start(audio_device_t *dev)
start playing.
Definition audio.h:30
void(*) set_volume(audio_device_t *dev, uint32_t left, uint32_t right)
set the output volume (optional).
Definition audio.h:50
void(*) stop(audio_device_t *dev)
stop playing.
Definition audio.h:35
the state for /proc/audio.
Definition audio.h:85
uint32_t volume
Definition audio.h:96
uint32_t rate
Definition audio.h:90
bool running
Definition audio.h:92
uint64_t played
Definition audio.h:95
bool present
Definition audio.h:86
uint32_t codec_id
Definition audio.h:88
uint32_t queued
Definition audio.h:93
const char * detail
Definition audio.h:89
const char * name
Definition audio.h:87
uint32_t underruns
Definition audio.h:94
uint32_t open
Definition audio.h:91