File: alsa.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (233 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#if	defined(ALSA_PLAYER)
/*
 * Copyright 1997 Jimen Ching And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained.
 * Jimen Ching And Sundry Contributors are not
 * responsible for the consequences of using this software.
 */

/* Direct to ALSA sound driver
 *
 * added by Jimen Ching (jching@flex.com) 19990207
 * based on info grabed from aplay.c in alsa-utils package.
 */

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/asound.h>
#include <sys/ioctl.h>
#include "st.h"
#include "libst.h"

char *get_style(style)
int style;
{
    switch(style)
    {
	case UNSIGNED: return "UNSIGNED";
	case SIGN2: return "SIGNED LINEAR 2's Comp";
	case ULAW: return "uLaw";
	case ALAW: return "aLaw";
	case ADPCM: return "ADPCM";
	default: return "unknown";
    }
}

/*
 * Do anything required before you start reading samples.
 * Read file header.
 *	Find out sampling rate,
 *	size and style of samples,
 *	mono/stereo/quad.
 */
void alsastartread(ft)
ft_t ft;
{
    int bps, fmt, size;
    snd_pcm_record_info_t r_info;
    snd_pcm_format_t format;
    snd_pcm_record_params_t r_params;

    memset(&r_info, 0, sizeof(r_info));
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_RECORD_INFO, &r_info);
    ft->file.count = 0;
    ft->file.pos = 0;
    ft->file.eof = 0;
    ft->file.size = r_info.buffer_size;
    if ((ft->file.buf = malloc (ft->file.size)) == NULL) {
	fail("unable to allocate output buffer of size %d", ft->file.size);
    }
    if (ft->info.rate < r_info.min_rate) ft->info.rate = 2 * r_info.min_rate;
    else if (ft->info.rate > r_info.max_rate) ft->info.rate = r_info.max_rate;
    if (ft->info.channels == -1) ft->info.channels = r_info.min_channels;
    else if (ft->info.channels > r_info.max_channels) ft->info.channels = r_info.max_channels;
    if (ft->info.size == -1) {
	if ((r_info.hw_formats & SND_PCM_FMT_U8) || (r_info.hw_formats & SND_PCM_FMT_S8))
	    ft->info.size = BYTE;
	else
	    ft->info.size = WORD;
    }
    if (ft->info.style == -1) {
	if ((r_info.hw_formats & SND_PCM_FMT_S16_LE) || (r_info.hw_formats & SND_PCM_FMT_S8))
	    ft->info.style = SIGN2;
	else
	    ft->info.style = UNSIGNED;
    }
    if (ft->info.size == BYTE) {
	switch (ft->info.style)
	{
	    case SIGN2:
		if (!(r_info.hw_formats & SND_PCM_FMT_S8))
		    fail("ALSA driver does not support signed byte samples");
		fmt = SND_PCM_SFMT_S8;
		break;
	    case UNSIGNED:
		if (!(r_info.hw_formats & SND_PCM_FMT_U8))
		    fail("ALSA driver does not support unsigned byte samples");
		fmt = SND_PCM_SFMT_U8;
		break;
	    default:
		fail("Hardware does not support %s output", get_style(ft->info.style));
		break;
	}
    }
    else {
	switch (ft->info.style)
	{
	    case SIGN2:
		if (!(r_info.hw_formats & SND_PCM_FMT_S16_LE))
		    fail("ALSA driver does not support signed word samples");
		fmt = SND_PCM_SFMT_S16_LE;
		break;
	    case UNSIGNED:
		if (!(r_info.hw_formats & SND_PCM_FMT_U16_LE))
		    fail("ALSA driver does not support unsigned word samples");
		fmt = SND_PCM_SFMT_U16_LE;
		break;
	    default:
		fail("Hardware does not support %s output", get_style(ft->info.style));
		break;
	}
    }

    memset(&format, 0, sizeof(format));
    format.format = fmt;
    format.rate = ft->info.rate;
    format.channels = ft->info.channels;
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_RECORD_FORMAT, &format);

    size = ft->file.size;
    bps = format.rate * format.channels;
    if (ft->info.size == WORD) bps <<= 1;
    bps >>= 2;
    while (size > bps) size >>= 1;
    if (size < 16) size = 16;
    memset(&r_params, 0, sizeof(r_params));
    r_params.fragment_size = size;
    r_params.fragments_min = 1;
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_RECORD_PARAMS, &r_params);

    /* Change to non-buffered I/O */
    setvbuf(ft->fp, NULL, _IONBF, sizeof(char) * ft->file.size);

    sigintreg(ft);	/* Prepare to catch SIGINT */
}

void alsastartwrite(ft)
ft_t ft;
{
    int bps, fmt, size;
    snd_pcm_playback_info_t p_info;
    snd_pcm_format_t format;
    snd_pcm_playback_params_t p_params;

    memset(&p_info, 0, sizeof(p_info));
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_PLAYBACK_INFO, &p_info);
    ft->file.pos = 0;
    ft->file.eof = 0;
    ft->file.size = p_info.buffer_size;
    if ((ft->file.buf = malloc (ft->file.size)) == NULL) {
	fail("unable to allocate output buffer of size %d", ft->file.size);
    }
    if (ft->info.rate < p_info.min_rate) ft->info.rate = 2 * p_info.min_rate;
    else if (ft->info.rate > p_info.max_rate) ft->info.rate = p_info.max_rate;
    if (ft->info.channels == -1) ft->info.channels = p_info.min_channels;
    else if (ft->info.channels > p_info.max_channels) ft->info.channels = p_info.max_channels;
    if (ft->info.size == -1) {
	if ((p_info.hw_formats & SND_PCM_FMT_U8) || (p_info.hw_formats & SND_PCM_FMT_S8))
	    ft->info.size = BYTE;
	else
	    ft->info.size = WORD;
    }
    if (ft->info.style == -1) {
	if ((p_info.hw_formats & SND_PCM_FMT_S16_LE) || (p_info.hw_formats & SND_PCM_FMT_S8))
	    ft->info.style = SIGN2;
	else
	    ft->info.style = UNSIGNED;
    }
    if (ft->info.size == BYTE) {
	switch (ft->info.style)
	{
	    case SIGN2:
		if (!(p_info.hw_formats & SND_PCM_FMT_S8))
		    fail("ALSA driver does not support signed byte samples");
		fmt = SND_PCM_SFMT_S8;
		break;
	    case UNSIGNED:
		if (!(p_info.hw_formats & SND_PCM_FMT_U8))
		    fail("ALSA driver does not support unsigned byte samples");
		fmt = SND_PCM_SFMT_U8;
		break;
	    default:
		fail("Hardware does not support %s output", get_style(ft->info.style));
		break;
	}
    }
    else {
	switch (ft->info.style)
	{
	    case SIGN2:
		if (!(p_info.hw_formats & SND_PCM_FMT_S16_LE))
		    fail("ALSA driver does not support signed word samples");
		fmt = SND_PCM_SFMT_S16_LE;
		break;
	    case UNSIGNED:
		if (!(p_info.hw_formats & SND_PCM_FMT_U16_LE))
		    fail("ALSA driver does not support unsigned word samples");
		fmt = SND_PCM_SFMT_U16_LE;
		break;
	    default:
		fail("Hardware does not support %s output", get_style(ft->info.style));
		break;
	}
    }

    memset(&format, 0, sizeof(format));
    format.format = fmt;
    format.rate = ft->info.rate;
    format.channels = ft->info.channels;
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_PLAYBACK_FORMAT, &format);

    size = ft->file.size;
    bps = format.rate * format.channels;
    if (ft->info.size == WORD) bps <<= 1;
    bps >>= 2;
    while (size > bps) size >>= 1;
    if (size < 16) size = 16;
    memset(&p_params, 0, sizeof(p_params));
    p_params.fragment_size = size;
    p_params.fragments_max = -1;
    p_params.fragments_room = 1;
    ioctl(fileno(ft->fp), SND_PCM_IOCTL_PLAYBACK_PARAMS, &p_params);

    /* Change to non-buffered I/O */
    setvbuf(ft->fp, NULL, _IONBF, sizeof(char) * ft->file.size);
}

#endif