-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpcm.c
More file actions
228 lines (171 loc) · 7.11 KB
/
pcm.c
File metadata and controls
228 lines (171 loc) · 7.11 KB
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
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <errno.h>
#include "./pcm.h"
#include "./con_msg.h"
#include "./main.h"
int pcm_config()
{
int val;
con_msg(MSG_VERBOSE, "\n***** PCM TYPES AND FORMAT *****\n");
con_msg(MSG_VERBOSE, "\nALSA Library Version: %s\n", SND_LIB_VERSION_STR);
con_msg(MSG_VERBOSE, "\nPCM Stream Types:\n");
for(val=0; val<= SND_PCM_STREAM_LAST; val++)
{
con_msg(MSG_VERBOSE, " %s\n", snd_pcm_stream_name( (snd_pcm_stream_t)val) );
}
con_msg(MSG_VERBOSE, "\nPCM Formats:\n");
for(val=0; val<= SND_PCM_FORMAT_LAST; val++)
{
if(snd_pcm_format_name( (snd_pcm_format_t)val ) != NULL)
{
con_msg(MSG_VERBOSE, " %s (%s)\n", snd_pcm_format_name( (snd_pcm_stream_t)val ),
snd_pcm_format_description( (snd_pcm_format_t)val )
);
}
}
con_msg(MSG_VERBOSE, "\nPCM Sub-Formats:\n");
for(val=0; val<= SND_PCM_SUBFORMAT_LAST; val++)
{
con_msg(MSG_VERBOSE, " %s (%s)\n", snd_pcm_subformat_name( (snd_pcm_subformat_t)val ),
snd_pcm_subformat_description( (snd_pcm_subformat_t)val )
);
}
con_msg(MSG_VERBOSE, "\nPCM States:\n");
for(val=0; val<= SND_PCM_STATE_LAST; val++)
{
con_msg(MSG_VERBOSE, " %s\n", snd_pcm_state_name( (snd_pcm_stream_t)val) );
}
return 0;
}
int pcm_setup_and_play(int fd)
{
con_msg(MSG_VERBOSE, "\n\n***** PCM SETUP *****\n");
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val, val2;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
int short_read_cnt = 0;
int i = 0;
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
con_msg(MSG_BAD, "unable to open pcm device: %s\n", snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0)
{
con_msg(MSG_BAD, "unable to set hw parameters: %s\n", snd_strerror(rc));
exit(1);
}
/* Display information about the PCM interface */
con_msg(MSG_VERBOSE, "PCM handle name = '%s'\n", snd_pcm_name(handle));
con_msg(MSG_VERBOSE, "PCM state = %s\n", snd_pcm_state_name(snd_pcm_state(handle)));
snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val);
con_msg(MSG_VERBOSE, "access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val));
snd_pcm_hw_params_get_format(params, &val);
con_msg(MSG_VERBOSE, "format = '%s' (%s)\n", snd_pcm_format_name((snd_pcm_format_t)val),
snd_pcm_format_description((snd_pcm_format_t)val));
snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
con_msg(MSG_VERBOSE, "subformat = '%s' (%s)\n", snd_pcm_subformat_name((snd_pcm_subformat_t)val),
snd_pcm_subformat_description((snd_pcm_subformat_t)val));
snd_pcm_hw_params_get_channels(params, &val);
con_msg(MSG_VERBOSE, "channels = %d\n", val);
snd_pcm_hw_params_get_rate(params, &val, &dir);
con_msg(MSG_VERBOSE, "rate = %d bps\n", val);
snd_pcm_hw_params_get_period_time(params, &val, &dir);
con_msg(MSG_VERBOSE, "period time = %d us\n", val);
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
con_msg(MSG_VERBOSE, "period size = %d frames\n", (int)frames);
snd_pcm_hw_params_get_buffer_time(params, &val, &dir);
con_msg(MSG_VERBOSE, "buffer time = %d us\n", val);
snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);
con_msg(MSG_VERBOSE, "buffer size = %d frames\n", val);
snd_pcm_hw_params_get_periods(params, &val, &dir);
con_msg(MSG_VERBOSE, "periods per buffer = %d frames\n", val);
snd_pcm_hw_params_get_rate_numden(params, &val, &val2);
con_msg(MSG_VERBOSE, "exact rate = %d/%d bps\n", val, val2);
val = snd_pcm_hw_params_get_sbits(params);
con_msg(MSG_VERBOSE, "significant bits = %d\n", val);
snd_pcm_hw_params_get_tick_time(params, &val, &dir);
con_msg(MSG_VERBOSE, "tick time = %d us\n", val);
val = snd_pcm_hw_params_is_batch(params);
con_msg(MSG_VERBOSE, "is batch = %d\n", val);
val = snd_pcm_hw_params_is_block_transfer(params);
con_msg(MSG_VERBOSE, "is block transfer = %d\n", val);
val = snd_pcm_hw_params_is_double(params);
con_msg(MSG_VERBOSE, "is double = %d\n", val);
val = snd_pcm_hw_params_is_half_duplex(params);
con_msg(MSG_VERBOSE, "is half duplex = %d\n", val);
val = snd_pcm_hw_params_is_joint_duplex(params);
con_msg(MSG_VERBOSE, "is joint duplex = %d\n", val);
val = snd_pcm_hw_params_can_overrange(params);
con_msg(MSG_VERBOSE, "can overrange = %d\n", val);
val = snd_pcm_hw_params_can_mmap_sample_resolution(params);
con_msg(MSG_VERBOSE, "can mmap = %d\n", val);
val = snd_pcm_hw_params_can_pause(params);
con_msg(MSG_VERBOSE, "can pause = %d\n", val);
val = snd_pcm_hw_params_can_resume(params);
con_msg(MSG_VERBOSE, "can resume = %d\n", val);
val = snd_pcm_hw_params_can_sync_start(params);
con_msg(MSG_VERBOSE, "can sync start = %d\n", val);
con_msg(MSG_VERBOSE, "***** PLAY FILE *****");
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
size = frames * 4; //2 bytes/sample, 2 channels
buffer = (char *)malloc(size);
printf("\n"); //Required, else last print message not displayed.
while(1)
{
rc = read(fd, buffer, size);
if (rc == 0)
{
break;
}
else if(rc != size)
{
(short_read_cnt==0) ? con_msg(MSG_BAD, "") : con_msg(MSG_BAD, "short read: read %d bytes\n", rc);
++short_read_cnt;
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE)
{ /* EPIPE means underrun */
con_msg(MSG_BAD, "underrun occurred\n");
snd_pcm_prepare(handle);
}
else if (rc < 0)
{
con_msg(MSG_BAD, "error from writei: %s\n", snd_strerror(rc));
}
else if (rc != (int)frames)
{
con_msg(MSG_BAD, "short write, write %d frames\n", rc);
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return fd;
}