1 /* Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * This example plays a file. The filename is the only argument. The file is
6 * assumed to contain raw stereo 16-bit PCM data to be played at 48kHz.
7 * usage: cplay <filename>
8 */
9
10 #include <cras_client.h>
11 #include <cras_helpers.h>
12 #include <sys/param.h>
13 #include <stdio.h>
14 #include <stdint.h>
15
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
18 struct cras_client *client;
19 cras_stream_id_t stream_id;
20 int rc = 0;
21 int fd;
22 const unsigned int num_channels = 2;
23 const unsigned int rate = 48000;
24 const unsigned int flags = 0;
25 uint8_t *buffer;
26 int nread;
27
28 if (argc < 2)
29 printf("Usage: %s filename\n", argv[0]);
30
31 fd = open(argv[1], O_RDONLY);
32 if (fd < 0) {
33 perror("failed to open file");
34 return -errno;
35 }
36
37 buffer = malloc(48000 * 4 * 5);
38
39 nread = read(fd, buffer, 48000 * 4 * 5);
40 if (nread <= 0) {
41 free(buffer);
42 close(fd);
43 return nread;
44 }
45
46 rc = cras_helper_create_connect(&client);
47 if (rc < 0) {
48 fprintf(stderr, "Couldn't create client.\n");
49 free(buffer);
50 close(fd);
51 return rc;
52 }
53
54 rc = cras_helper_play_buffer(client, buffer, nread / 4,
55 SND_PCM_FORMAT_S16_LE, rate, num_channels,
56 cras_client_get_first_dev_type_idx(
57 client, CRAS_NODE_TYPE_INTERNAL_SPEAKER,
58 CRAS_STREAM_OUTPUT));
59 if (rc < 0) {
60 fprintf(stderr, "playing a buffer %d\n", rc);
61 goto destroy_exit;
62 }
63
64 /* At this point the stream has been added and audio callbacks will
65 * start to fire. This app can now go off and do other things, but this
66 * example just loops forever. */
67 while (1) {
68 sleep(1);
69 }
70
71 destroy_exit:
72 cras_client_destroy(client);
73 free(buffer);
74 close(fd);
75 return rc;
76 }
77