| [e677b08] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup wavplay
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file PCM playback audio devices
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <fibril_synch.h>
|
|---|
| [5cd5079] | 39 | #include <str_error.h>
|
|---|
| [e677b08] | 40 | #include <stdio.h>
|
|---|
| [5cd5079] | 41 | #include <hound/client.h>
|
|---|
| [ea6c838] | 42 | #include <pcm/sample_format.h>
|
|---|
| [aef1799] | 43 | #include <getopt.h>
|
|---|
| [e677b08] | 44 |
|
|---|
| [aef1799] | 45 | #include "dplay.h"
|
|---|
| [e677b08] | 46 | #include "wave.h"
|
|---|
| 47 |
|
|---|
| [4389076] | 48 | #define BUFFER_SIZE (32 * 1024)
|
|---|
| [abaef81] | 49 |
|
|---|
| 50 |
|
|---|
| 51 | static int hplay(const char *filename)
|
|---|
| 52 | {
|
|---|
| [60e5696d] | 53 | printf("Hound playback: %s\n", filename);
|
|---|
| [abaef81] | 54 | FILE *source = fopen(filename, "rb");
|
|---|
| [bd5860f] | 55 | if (!source) {
|
|---|
| 56 | printf("Failed to open file %s\n", filename);
|
|---|
| [abaef81] | 57 | return EINVAL;
|
|---|
| [bd5860f] | 58 | }
|
|---|
| [abaef81] | 59 | wave_header_t header;
|
|---|
| 60 | size_t read = fread(&header, sizeof(header), 1, source);
|
|---|
| [bd5860f] | 61 | if (read != 1) {
|
|---|
| 62 | printf("Failed to read WAV header: %zu\n", read);
|
|---|
| [abaef81] | 63 | fclose(source);
|
|---|
| 64 | return EIO;
|
|---|
| 65 | }
|
|---|
| [9e1800c] | 66 | pcm_format_t format;
|
|---|
| [abaef81] | 67 | const char *error;
|
|---|
| [9e1800c] | 68 | int ret = wav_parse_header(&header, NULL, NULL, &format.channels,
|
|---|
| 69 | &format.sampling_rate, &format.sample_format, &error);
|
|---|
| [abaef81] | 70 | if (ret != EOK) {
|
|---|
| 71 | printf("Error parsing wav header: %s.\n", error);
|
|---|
| 72 | fclose(source);
|
|---|
| 73 | return EINVAL;
|
|---|
| 74 | }
|
|---|
| 75 | hound_context_t *hound = hound_context_create_playback(filename,
|
|---|
| [4389076] | 76 | format, BUFFER_SIZE * 2);
|
|---|
| [abaef81] | 77 | if (!hound) {
|
|---|
| 78 | printf("Failed to create HOUND context\n");
|
|---|
| 79 | fclose(source);
|
|---|
| 80 | return ENOMEM;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| [ec81221] | 83 | ret = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
|
|---|
| [abaef81] | 84 | if (ret != EOK) {
|
|---|
| 85 | printf("Failed to connect to default target: %s\n",
|
|---|
| 86 | str_error(ret));
|
|---|
| [62beb4b] | 87 | hound_context_destroy(hound);
|
|---|
| [abaef81] | 88 | fclose(source);
|
|---|
| [62beb4b] | 89 | return ret;
|
|---|
| [abaef81] | 90 | }
|
|---|
| 91 | static char buffer[BUFFER_SIZE];
|
|---|
| [bd5860f] | 92 | while ((read = fread(buffer, sizeof(char), BUFFER_SIZE, source)) > 0) {
|
|---|
| 93 | ret = hound_write_main_stream(hound, buffer, read);
|
|---|
| [abaef81] | 94 | if (ret != EOK) {
|
|---|
| [bd5860f] | 95 | printf("Failed to write to main context stream: %s\n",
|
|---|
| [abaef81] | 96 | str_error(ret));
|
|---|
| 97 | break;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| [03c2d5f] | 100 | hound_context_destroy(hound);
|
|---|
| [abaef81] | 101 | fclose(source);
|
|---|
| 102 | return ret;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [aef1799] | 105 | static const struct option opts[] = {
|
|---|
| 106 | {"device", required_argument, 0, 'd'},
|
|---|
| 107 | {"record", no_argument, 0, 'r'},
|
|---|
| [86fe9d1] | 108 | {"help", no_argument, 0, 'h'},
|
|---|
| [aef1799] | 109 | {0, 0, 0, 0}
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| [86fe9d1] | 112 | static void print_help(const char* name)
|
|---|
| 113 | {
|
|---|
| [19f6ea5b] | 114 | printf("Usage: %s [options] file [files...]\n", name);
|
|---|
| [86fe9d1] | 115 | printf("supported options:\n");
|
|---|
| 116 | printf("\t -h, --help\t Print this help.\n");
|
|---|
| [60e5696d] | 117 | printf("\t -r, --record\t Start recording instead of playback. "
|
|---|
| 118 | "(Not implemented)\n");
|
|---|
| 119 | printf("\t -d, --device\t Use specified device instead of the sound "
|
|---|
| 120 | "service. Use location path or a special device `default'\n");
|
|---|
| [86fe9d1] | 121 | }
|
|---|
| [aef1799] | 122 |
|
|---|
| [e677b08] | 123 | int main(int argc, char *argv[])
|
|---|
| 124 | {
|
|---|
| [aef1799] | 125 | const char *device = "default";
|
|---|
| 126 | int idx = 0;
|
|---|
| 127 | bool direct = false, record = false;
|
|---|
| 128 | optind = 0;
|
|---|
| 129 | int ret = 0;
|
|---|
| 130 | while (ret != -1) {
|
|---|
| [86fe9d1] | 131 | ret = getopt_long(argc, argv, "d:rh", opts, &idx);
|
|---|
| [aef1799] | 132 | switch (ret) {
|
|---|
| 133 | case 'd':
|
|---|
| 134 | direct = true;
|
|---|
| 135 | device = optarg;
|
|---|
| 136 | break;
|
|---|
| 137 | case 'r':
|
|---|
| 138 | record = true;
|
|---|
| 139 | break;
|
|---|
| [86fe9d1] | 140 | case 'h':
|
|---|
| 141 | print_help(*argv);
|
|---|
| 142 | return 0;
|
|---|
| [aef1799] | 143 | };
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | if (optind == argc) {
|
|---|
| 147 | printf("Not enough arguments.\n");
|
|---|
| [86fe9d1] | 148 | print_help(*argv);
|
|---|
| [e677b08] | 149 | return 1;
|
|---|
| [aef1799] | 150 | }
|
|---|
| 151 |
|
|---|
| [19f6ea5b] | 152 | for (int i = optind; i < argc; ++i) {
|
|---|
| 153 | const char *file = argv[i];
|
|---|
| 154 |
|
|---|
| 155 | printf("%s (%d/%d) %s\n", record ? "Recording" : "Playing",
|
|---|
| 156 | i - optind + 1, argc - optind, file);
|
|---|
| 157 | if (record) {
|
|---|
| 158 | printf("Recording is not supported yet.\n");
|
|---|
| 159 | return 1;
|
|---|
| 160 | }
|
|---|
| 161 | if (direct) {
|
|---|
| 162 | dplay(device, file);
|
|---|
| 163 | } else {
|
|---|
| 164 | hplay(file);
|
|---|
| 165 | }
|
|---|
| [4389076] | 166 | }
|
|---|
| [19f6ea5b] | 167 | return 0;
|
|---|
| [e677b08] | 168 | }
|
|---|
| 169 | /**
|
|---|
| 170 | * @}
|
|---|
| 171 | */
|
|---|