• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
58 					     CRAS_NODE_TYPE_INTERNAL_SPEAKER,
59 					     CRAS_STREAM_OUTPUT));
60 	if (rc < 0) {
61 		fprintf(stderr, "playing a buffer %d\n", rc);
62 		goto destroy_exit;
63 	}
64 
65 	/* At this point the stream has been added and audio callbacks will
66 	 * start to fire.  This app can now go off and do other things, but this
67 	 * example just loops forever. */
68 	while (1) {
69 		sleep(1);
70 	}
71 
72 destroy_exit:
73 	cras_client_destroy(client);
74 	free(buffer);
75 	close(fd);
76 	return rc;
77 }
78