-
Notifications
You must be signed in to change notification settings - Fork 1
/
sound_global.h
205 lines (171 loc) · 4.63 KB
/
sound_global.h
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
/*
* simple Linux sound library
* Copyright (C) 2017, 2018 Ricardo Biehl Pasquali
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOUND_GLOBAL_H
#define SOUND_GLOBAL_H
/* ALSA header */
#include <sound/asound.h>
#include "sound_open_device.h"
/*
* configuration flags
* ===================
*
* The first four bits are reserved to open flags
*/
/* request MMAP access to sound buffer */
#define SND_MMAP 0x00000010
/* disable device interrupts */
#define SND_NOIRQ 0x00000020
/* use CLOCK_MONOTONIC for timestamps */
#define SND_MONOTONIC 0x00000040
/*
* snd states
* ==========
*/
#define SND_STATE_OPEN SNDRV_PCM_STATE_OPEN
#define SND_STATE_SETUP SNDRV_PCM_STATE_SETUP
#define SND_STATE_PREPARED SNDRV_PCM_STATE_PREPARED
#define SND_STATE_RUNNING SNDRV_PCM_STATE_RUNNING
#define SND_STATE_PAUSED SNDRV_PCM_STATE_PAUSED
#define SND_STATE_XRUN SNDRV_PCM_STATE_XRUN /* (over/under)run */
#define SND_STATE_DRAINING SNDRV_PCM_STATE_DRAINING
#define SND_STATE_SUSPENDED SNDRV_PCM_STATE_SUSPENDED
#define SND_STATE_DISCONNECTED SNDRV_PCM_STATE_DISCONNECTED
/*
* snd configuration structure for snd_open()
* ==========================================
*/
struct snd_config {
/*
* General flags
* =============
*
* Capture or playback? NONBLOCK? mmap? Interrupts?
*/
unsigned int flags;
/*
* Opening configuration
* =====================
*/
unsigned int card;
unsigned int device;
/*
* Hardware parameters
* ===================
*/
unsigned int format;
unsigned int channels;
unsigned int rate;
unsigned int period_size;
unsigned int period_count;
/*
* Software parameters
* ===================
*/
/* minimum available for application wake up */
unsigned long avail_min;
/*
* number of frames that, in playback, application
* needs to write, in capture, application must
* try to read, in order to start sound device.
*/
unsigned long start_threshold;
/*
* stop_threshold: number of available frames to
* sound device enter in xrun state.
*
* NOTE: As xruns are not being handled, it's set
* to boundary. However, xruns can still occur when
* device returns -1 as its buffer position.
*/
unsigned long stop_threshold;
/*
* Playback only. Frames to fill with silence
* ahead of application pointer.
*/
unsigned long silence_threshold;
};
/*
* The sound structure
* ===================
*
* This is the main structure of this sound library. It
* contains the file descriptor, parameters, and
* memory mapping info.
*/
struct snd {
/* file descriptor for the PCM */
int fd;
/* OUTPUT or INPUT */
unsigned int type;
unsigned int bytes_per_frame;
unsigned int buffer_size; /* frames */
unsigned long boundary; /* frames */
ssize_t (*transfer) (struct snd*, void*, unsigned int);
/*
* Synchronization structures
* ==========================
*/
/*
* When mmap of status or control areas fails,
* sync_ptr points to the memory allocated to
* synchronize pointers via ioctl. If mmap was
* successful, sync_ptr is set to NULL.
*
* status and control point to an offset of mmaped
* or allocated memory.
*/
struct snd_pcm_mmap_status *status;
struct snd_pcm_mmap_control *control;
struct snd_pcm_sync_ptr *sync_ptr;
/*
* MMAP data buffer
* ================
*
* Only when MMAP transfer
*/
/* sound device buffer to transfer audio */
void *mmap_buffer;
};
static inline int
snd_is_running(struct snd *pcm)
{
return (pcm->status->state == SND_STATE_RUNNING ||
(pcm->status->state == SND_STATE_DRAINING &&
pcm->type == SND_OUTPUT));
}
#if 0
static inline int
snd_is_empty(struct snd *pcm)
{
if (pcm->type & SND_INPUT)
return pcm->avail == 0;
else
return pcm->avail >= pcm->buffer_size;
}
#endif
static inline unsigned int
snd_bytes_to_frames(const struct snd *pcm, unsigned int bytes)
{
return bytes / pcm->bytes_per_frame;
}
static inline unsigned int
snd_frames_to_bytes(const struct snd *pcm, unsigned int frames)
{
return frames * pcm->bytes_per_frame;
}
#endif /* SOUND_GLOBAL_H */