blob: 2a8eefdfae54fbca74574d2eba7955dddb32183b [file] [log] [blame]
Steve Muckle0a9c0872022-02-16 05:58:07 +00001/*******************************************************************************
2* Copyright (C) 2018 Cadence Design Systems, Inc.
3*
4* Permission is hereby granted, free of charge, to any person obtaining
5* a copy of this software and associated documentation files (the
6* "Software"), to use this Software with Cadence processor cores only and
7* not with any other processors and platforms, subject to
8* the following conditions:
9*
10* The above copyright notice and this permission notice shall be included
11* in all copies or substantial portions of the Software.
12*
13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21******************************************************************************/
22
23#define MODULE_TAG PLYBK
24
25#include <tinyalsa/asoundlib.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h>
30#include <signal.h>
31#include <sys/ioctl.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <errno.h>
35
36#include "xf.h"
37#include "xa_playback.h"
38
39#if 0
40#define ID_RIFF 0x46464952
41#define ID_WAVE 0x45564157
42#define ID_FMT 0x20746d66
43#define ID_DATA 0x61746164
44
45#define HIFI_MISC_IOCTL_PCM_GAIN _IOW('A', 0x7b, struct misc_io_pcm_buf_param)
46#define HIFI_DSP_MISC_DRIVER "/dev/hifi_misc"
47struct misc_io_pcm_buf_param {
48 uint64_t buf;
49 uint32_t buf_size;
50};
51
52struct riff_wave_header {
53 uint32_t riff_id;
54 uint32_t riff_sz;
55 uint32_t wave_id;
56};
57
58struct chunk_header {
59 uint32_t id;
60 uint32_t sz;
61};
62
63struct chunk_fmt {
64 uint16_t audio_format;
65 uint16_t num_channels;
66 uint32_t sample_rate;
67 uint32_t byte_rate;
68 uint16_t block_align;
69 uint16_t bits_per_sample;
70};
71
72static int s_close = 0;
73
74void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
75 unsigned int rate, unsigned int bits, unsigned int period_size,
76 unsigned int period_count);
77
78void stream_close(int sig)
79{
80 /* allow the stream to be closed gracefully */
81 signal(sig, SIG_IGN);
82 s_close = 1;
83}
84#endif
85
86#if 0
87int main(int argc, char **argv)
88{
89 FILE *file;
90 struct riff_wave_header riff_wave_header;
91 struct chunk_header chunk_header;
92 struct chunk_fmt chunk_fmt;
93 unsigned int device = 0;
94 unsigned int card = 0;
95 unsigned int period_size = 1024;
96 unsigned int period_count = 4;
97 char *filename;
98 int more_chunks = 1;
99
100 if (argc < 2) {
101 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-p period_size]"
102 " [-n n_periods] \n", argv[0]);
103 return 1;
104 }
105
106 filename = argv[1];
107 file = fopen(filename, "rb");
108 if (!file) {
109 fprintf(stderr, "Unable to open file '%s'\n", filename);
110 return 1;
111 }
112
113 fread(&riff_wave_header, sizeof(riff_wave_header), 1, file);
114 if ((riff_wave_header.riff_id != ID_RIFF) ||
115 (riff_wave_header.wave_id != ID_WAVE)) {
116 fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename);
117 fclose(file);
118 return 1;
119 }
120
121 do {
122 fread(&chunk_header, sizeof(chunk_header), 1, file);
123
124 switch (chunk_header.id) {
125 case ID_FMT:
126 fread(&chunk_fmt, sizeof(chunk_fmt), 1, file);
127 /* If the format header is larger, skip the rest */
128 if (chunk_header.sz > sizeof(chunk_fmt))
129 fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
130 break;
131 case ID_DATA:
132 /* Stop looking for chunks */
133 more_chunks = 0;
134 break;
135 default:
136 /* Unknown chunk, skip bytes */
137 fseek(file, chunk_header.sz, SEEK_CUR);
138 }
139 } while (more_chunks);
140
141 /* parse command line arguments */
142 argv += 2;
143 while (*argv) {
144 if (strcmp(*argv, "-d") == 0) {
145 argv++;
146 if (*argv)
147 device = atoi(*argv);
148 }
149 if (strcmp(*argv, "-p") == 0) {
150 argv++;
151 if (*argv)
152 period_size = atoi(*argv);
153 }
154 if (strcmp(*argv, "-n") == 0) {
155 argv++;
156 if (*argv)
157 period_count = atoi(*argv);
158 }
159 if (strcmp(*argv, "-D") == 0) {
160 argv++;
161 if (*argv)
162 card = atoi(*argv);
163 }
164 if (*argv)
165 argv++;
166 }
167
168 play_sample(file, card, device, chunk_fmt.num_channels, chunk_fmt.sample_rate,
169 chunk_fmt.bits_per_sample, period_size, period_count);
170
171 fclose(file);
172
173 return 0;
174}
175#endif
176
177static int check_param(struct pcm_params *params, unsigned int param, unsigned int value,
178 char *param_name, char *param_unit)
179{
180 unsigned int min;
181 unsigned int max;
182 int is_within_bounds = 1;
183
184 min = pcm_params_get_min(params, param);
185 if (value < min) {
186 TRACE(ERROR, _x("%s is %u%s, device only supports >= %u%s\n"), param_name, value,
187 param_unit, min, param_unit);
188 is_within_bounds = 0;
189 }
190
191 max = pcm_params_get_max(params, param);
192 if (value > max) {
193 TRACE(ERROR, _x("%s is %u%s, device only supports <= %u%s\n"), param_name, value,
194 param_unit, max, param_unit);
195 is_within_bounds = 0;
196 }
197
198 return is_within_bounds;
199}
200
201static int sample_is_playable(unsigned int card, unsigned int device, unsigned int channels,
202 unsigned int rate, unsigned int bits, unsigned int period_size,
203 unsigned int period_count)
204{
205 struct pcm_params *params;
206 int can_play;
207
208 params = pcm_params_get(card, device, PCM_OUT);
209 if (params == NULL) {
210 TRACE(ERROR, _x("Unable to open PCM device %u.\n"), device);
211 return 0;
212 }
213
214 can_play = check_param(params, PCM_PARAM_RATE, rate, "Sample rate", "Hz");
215 can_play &= check_param(params, PCM_PARAM_CHANNELS, channels, "Sample", " channels");
216 can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitrate", " bits");
217 can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, period_size, "Period size", "Hz");
218 can_play &= check_param(params, PCM_PARAM_PERIODS, period_count, "Period count", "Hz");
219
220 pcm_params_free(params);
221
222 return can_play;
223}
224
225void *xa_playback_open(unsigned int card,
226 unsigned int device,
227 unsigned int channels,
228 unsigned int rate,
229 unsigned int bits,
230 unsigned int period_size,
231 unsigned int period_count)
232{
233 struct pcm_config config;
234 struct pcm *pcm;
235
236 memset(&config, 0, sizeof(config));
237 config.channels = channels;
238 config.rate = rate;
239 config.period_size = period_size;
240 config.period_count = period_count;
241 if (bits == 32)
242 config.format = PCM_FORMAT_S32_LE;
243 else if (bits == 16)
244 config.format = PCM_FORMAT_S16_LE;
245 config.start_threshold = 0;
246 config.stop_threshold = 0;
247 config.silence_threshold = 0;
248
249 if (!sample_is_playable(card, device, channels, rate, bits, period_size, period_count)) {
250 return NULL;
251 }
252
253 pcm = pcm_open(card, device, PCM_OUT, &config);
254 if (!pcm || !pcm_is_ready(pcm)) {
255 TRACE(ERROR, _x("Unable to open PCM device %u (%s)\n"),
256 device, pcm_get_error(pcm));
257 return NULL;
258 }
259
260 return pcm;
261}
262
263int xa_playback_buf(void *handle,
264 const void *data,
265 unsigned int nbytes)
266{
267 int err;
268
269 if (!handle || !data)
270 return XA_PLAYBACK_INVALID_PARAM;
271
272 if (nbytes > 0) {
273 err = pcm_write(handle, data, nbytes);
274
275 if (err == -EINVAL) return XA_PLAYBACK_INVALID_PARAM;
276 if (err == -EPIPE ) return XA_PLAYBACK_UNDERRUN;
277 }
278
279 return XA_PLAYBACK_OK;
280}
281
282int xa_playback_close(void *handle)
283{
284 return pcm_close(handle);
285}
286
287#if 0
288void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
289 unsigned int rate, unsigned int bits, unsigned int period_size,
290 unsigned int period_count)
291{
292 struct pcm_config config;
293 struct pcm *pcm;
294 char *buffer;
295 int size;
296 int num_read;
297 int hifi_dsp_fd;
298 struct misc_io_pcm_buf_param pcmbuf;
299
300 hifi_dsp_fd = open(HIFI_DSP_MISC_DRIVER, O_RDWR, 0);
301 if(hifi_dsp_fd < 0){
302 printf("Error opening hifi dsp device %d", errno);
303 }
304
305 memset(&config, 0, sizeof(config));
306 config.channels = channels;
307 config.rate = rate;
308 config.period_size = period_size;
309 config.period_count = period_count;
310 if (bits == 32)
311 config.format = PCM_FORMAT_S32_LE;
312 else if (bits == 16)
313 config.format = PCM_FORMAT_S16_LE;
314 config.start_threshold = 0;
315 config.stop_threshold = 0;
316 config.silence_threshold = 0;
317
318 if (!sample_is_playable(card, device, channels, rate, bits, period_size, period_count)) {
319 return;
320 }
321
322 pcm = pcm_open(card, device, PCM_OUT, &config);
323 if (!pcm || !pcm_is_ready(pcm)) {
324 fprintf(stderr, "Unable to open PCM device %u (%s)\n",
325 device, pcm_get_error(pcm));
326 return;
327 }
328
329 size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
330 buffer = malloc(size);
331 if (!buffer) {
332 fprintf(stderr, "Unable to allocate %d bytes\n", size);
333 free(buffer);
334 pcm_close(pcm);
335 return;
336 }
337
338 printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
339
340 /* catch ctrl-c to shutdown cleanly */
341 signal(SIGINT, stream_close);
342
343 do {
344 num_read = fread(buffer, 1, size, file);
345
346 pcmbuf.buf =(uint64_t) buffer;
347 pcmbuf.buf_size = num_read;
348 if(hifi_dsp_fd) {
349 // printf("ioctl send \n");
350 ioctl(hifi_dsp_fd,HIFI_MISC_IOCTL_PCM_GAIN, &pcmbuf);
351 // printf("ioctl complete \n");
352 }
353
354 if (num_read > 0) {
355 if (pcm_write(pcm, buffer, num_read)) {
356 fprintf(stderr, "Error playing sample\n");
357 break;
358 }
359 }
360 } while (!s_close && num_read > 0);
361
362 free(buffer);
363 pcm_close(pcm);
364 close(hifi_dsp_fd);
365}
366#endif