2 * A simple PCM loopback utility
3 * Copyright (c) 2010 by Jaroslav Kysela <perex@perex.cz>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_SAMPLERATE_H
23 #define USE_SAMPLERATE
24 #include <samplerate.h>
27 SRC_SINC_BEST_QUALITY = 0,
28 SRC_SINC_MEDIUM_QUALITY = 1,
30 SRC_ZERO_ORDER_HOLD = 3,
39 #define FILE_PWRITE "/tmp/alsaloop.praw"
40 #define FILE_CWRITE "/tmp/alsaloop.craw"
43 #define WORKAROUND_SERIALOPEN (1<<0)
45 typedef enum _sync_type {
47 SYNC_TYPE_SIMPLE, /* add or remove samples */
48 SYNC_TYPE_CAPTRATESHIFT,
49 SYNC_TYPE_PLAYRATESHIFT,
51 SYNC_TYPE_AUTO, /* order: CAPTRATESHIFT, PLAYRATESHIFT, */
52 /* SAMPLERATE, SIMPLE */
53 SYNC_TYPE_LAST = SYNC_TYPE_AUTO
56 typedef enum _slave_type {
60 SLAVE_TYPE_LAST = SLAVE_TYPE_OFF
63 struct loopback_control {
64 snd_ctl_elem_id_t *id;
65 snd_ctl_elem_info_t *info;
66 snd_ctl_elem_value_t *value;
69 struct loopback_mixer {
71 struct loopback_control src;
72 struct loopback_control dst;
73 struct loopback_mixer *next;
76 struct loopback_ossmixer {
81 struct loopback_ossmixer *next;
84 struct loopback_handle {
85 struct loopback *loopback;
91 snd_pcm_access_t access;
92 snd_pcm_format_t format;
94 unsigned int rate_req;
95 unsigned int channels;
96 unsigned int buffer_size;
97 unsigned int period_size;
98 snd_pcm_uframes_t avail_min;
99 unsigned int buffer_size_req;
100 unsigned int period_size_req;
101 unsigned int frame_size;
102 unsigned int resample:1; /* do resample */
103 unsigned int nblock:1; /* do block (period size) transfers */
104 unsigned int xrun_pending:1;
105 unsigned int pollfd_count;
107 char *buf; /* I/O buffer */
108 snd_pcm_uframes_t buf_pos; /* I/O position */
109 snd_pcm_uframes_t buf_count; /* filled samples */
110 snd_pcm_uframes_t buf_size; /* buffer size in frames */
111 snd_pcm_uframes_t buf_over; /* capture buffer overflow */
113 snd_pcm_uframes_t max;
114 unsigned long long counter;
115 unsigned long sync_point; /* in samples */
116 snd_pcm_sframes_t last_delay;
118 snd_pcm_uframes_t total_queued;
121 unsigned int ctl_pollfd_count;
122 snd_ctl_elem_value_t *ctl_notify;
123 snd_ctl_elem_value_t *ctl_rate_shift;
124 snd_ctl_elem_value_t *ctl_active;
125 snd_ctl_elem_value_t *ctl_format;
126 snd_ctl_elem_value_t *ctl_rate;
127 snd_ctl_elem_value_t *ctl_channels;
132 struct loopback_handle *capt;
133 struct loopback_handle *play;
134 snd_pcm_uframes_t latency; /* final latency in frames */
135 unsigned int latency_req; /* in frames */
136 unsigned int latency_reqtime; /* in us */
137 unsigned long loop_time; /* ~0 = unlimited (in seconds) */
138 unsigned long long loop_limit; /* ~0 = unlimited (in frames) */
139 snd_output_t *output;
142 int active_pollfd_count;
143 unsigned int linked:1; /* linked streams */
144 unsigned int reinit:1;
145 unsigned int running:1;
146 unsigned int stop_pending:1;
147 snd_pcm_uframes_t stop_count;
148 sync_type_t sync; /* type of sync */
150 int thread; /* thread number */
155 snd_pcm_sframes_t pitch_diff;
156 snd_pcm_sframes_t pitch_diff_min;
157 snd_pcm_sframes_t pitch_diff_max;
158 unsigned int total_queued_count;
159 snd_timestamp_t tstamp_start;
160 snd_timestamp_t tstamp_end;
162 unsigned int xrun:1; /* xrun profiling */
163 snd_timestamp_t xrun_last_update;
164 snd_timestamp_t xrun_last_wake0;
165 snd_timestamp_t xrun_last_wake;
166 snd_timestamp_t xrun_last_check0;
167 snd_timestamp_t xrun_last_check;
168 snd_pcm_sframes_t xrun_last_pdelay;
169 snd_pcm_sframes_t xrun_last_cdelay;
170 snd_pcm_uframes_t xrun_buf_pcount;
171 snd_pcm_uframes_t xrun_buf_ccount;
172 unsigned int xrun_out_frames;
173 long xrun_max_proctime;
174 double xrun_max_missing;
176 struct loopback_mixer *controls;
177 struct loopback_ossmixer *oss_controls;
179 unsigned int use_samplerate:1;
180 #ifdef USE_SAMPLERATE
181 unsigned int src_enable:1;
182 int src_converter_type;
183 SRC_STATE *src_state;
185 unsigned int src_out_frames;
196 extern int workarounds;
197 extern int use_syslog;
199 #define logit(priority, fmt, args...) do { \
201 syslog(priority, fmt, ##args); \
203 fprintf(stderr, fmt, ##args); \
206 int pcmjob_init(struct loopback *loop);
207 int pcmjob_done(struct loopback *loop);
208 int pcmjob_start(struct loopback *loop);
209 int pcmjob_stop(struct loopback *loop);
210 int pcmjob_pollfds_init(struct loopback *loop, struct pollfd *fds);
211 int pcmjob_pollfds_handle(struct loopback *loop, struct pollfd *fds);
212 void pcmjob_state(struct loopback *loop);
214 int control_parse_id(const char *str, snd_ctl_elem_id_t *id);
215 int control_id_match(snd_ctl_elem_id_t *id1, snd_ctl_elem_id_t *id2);
216 int control_init(struct loopback *loop);
217 int control_done(struct loopback *loop);
218 int control_event(struct loopback_handle *lhandle, snd_ctl_event_t *ev);