[0a4ad7d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 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>
|
---|
[fec7ba0] | 38 | #include <fibril.h>
|
---|
[0a4ad7d] | 39 | #include <str_error.h>
|
---|
| 40 | #include <audio_pcm_iface.h>
|
---|
| 41 | #include <pcm/format.h>
|
---|
| 42 | #include <stdio.h>
|
---|
[b6e481b] | 43 | #include <as.h>
|
---|
[e5bc912] | 44 | #include <inttypes.h>
|
---|
[1d6dd2a] | 45 | #include <str.h>
|
---|
[0a4ad7d] | 46 |
|
---|
| 47 | #include "wave.h"
|
---|
| 48 | #include "drec.h"
|
---|
| 49 |
|
---|
[0e4c5f0] | 50 | #define BUFFER_PARTS 16
|
---|
[0a4ad7d] | 51 |
|
---|
| 52 | /** Recording format */
|
---|
| 53 | static const pcm_format_t format = {
|
---|
| 54 | .sampling_rate = 44100,
|
---|
| 55 | .channels = 2,
|
---|
| 56 | .sample_format = PCM_SAMPLE_SINT16_LE,
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | /** Recording helper structure */
|
---|
| 60 | typedef struct {
|
---|
| 61 | struct {
|
---|
| 62 | void *base;
|
---|
| 63 | size_t size;
|
---|
| 64 | unsigned id;
|
---|
[3bacee1] | 65 | void *position;
|
---|
[0a4ad7d] | 66 | } buffer;
|
---|
[3bacee1] | 67 | FILE *file;
|
---|
[0a4ad7d] | 68 | audio_pcm_sess_t *device;
|
---|
| 69 | } record_t;
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Initialize recording helper structure.
|
---|
| 73 | * @param rec Recording structure.
|
---|
| 74 | * @param sess Session to IPC device.
|
---|
| 75 | */
|
---|
| 76 | static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
|
---|
| 77 | {
|
---|
| 78 | assert(sess);
|
---|
| 79 | assert(rec);
|
---|
| 80 | rec->buffer.base = NULL;
|
---|
| 81 | rec->buffer.size = 0;
|
---|
| 82 | rec->buffer.position = NULL;
|
---|
| 83 | rec->file = NULL;
|
---|
| 84 | rec->device = sess;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[984a9ba] | 87 | /** Recording callback.
|
---|
[a46e56b] | 88 | *
|
---|
| 89 | * Writes recorded data.
|
---|
[b752a31] | 90 | *
|
---|
[c477c80] | 91 | * @param icall Pointer to IPC call structure.
|
---|
| 92 | * @param arg Argument. Pointer to recording helper structure.
|
---|
[984a9ba] | 93 | *
|
---|
[0a4ad7d] | 94 | */
|
---|
[984a9ba] | 95 | static void device_event_callback(ipc_call_t *icall, void *arg)
|
---|
[0a4ad7d] | 96 | {
|
---|
| 97 | record_t *rec = arg;
|
---|
| 98 | const size_t buffer_part = rec->buffer.size / BUFFER_PARTS;
|
---|
| 99 | bool record = true;
|
---|
[984a9ba] | 100 |
|
---|
[0a4ad7d] | 101 | while (record) {
|
---|
| 102 | ipc_call_t call;
|
---|
[984a9ba] | 103 | async_get_call(&call);
|
---|
| 104 |
|
---|
[eb13ef8] | 105 | switch (IPC_GET_IMETHOD(&call)) {
|
---|
[0a4ad7d] | 106 | case PCM_EVENT_CAPTURE_TERMINATED:
|
---|
| 107 | printf("Recording terminated\n");
|
---|
| 108 | record = false;
|
---|
[0e4c5f0] | 109 | break;
|
---|
[0a4ad7d] | 110 | case PCM_EVENT_FRAMES_CAPTURED:
|
---|
[eb13ef8] | 111 | printf("%" PRIun " frames\n", IPC_GET_ARG1(&call));
|
---|
[0a4ad7d] | 112 | break;
|
---|
| 113 | default:
|
---|
[eb13ef8] | 114 | printf("Unknown event %" PRIun ".\n", IPC_GET_IMETHOD(&call));
|
---|
[984a9ba] | 115 | async_answer_0(&call, ENOTSUP);
|
---|
[0a4ad7d] | 116 | continue;
|
---|
[0e4c5f0] | 117 | }
|
---|
[0a4ad7d] | 118 |
|
---|
[0e4c5f0] | 119 | if (!record) {
|
---|
[984a9ba] | 120 | async_answer_0(&call, EOK);
|
---|
[0e4c5f0] | 121 | break;
|
---|
[0a4ad7d] | 122 | }
|
---|
| 123 |
|
---|
| 124 | /* Write directly from device buffer to file */
|
---|
| 125 | const size_t bytes = fwrite(rec->buffer.position,
|
---|
[3bacee1] | 126 | sizeof(uint8_t), buffer_part, rec->file);
|
---|
[0a4ad7d] | 127 | printf("%zu ", bytes);
|
---|
| 128 | rec->buffer.position += buffer_part;
|
---|
| 129 |
|
---|
| 130 | if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
|
---|
| 131 | rec->buffer.position = rec->buffer.base;
|
---|
[984a9ba] | 132 | async_answer_0(&call, EOK);
|
---|
[0a4ad7d] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Start fragment based recording.
|
---|
| 138 | * @param rec Recording helper structure.
|
---|
| 139 | * @param f PCM format
|
---|
| 140 | */
|
---|
| 141 | static void record_fragment(record_t *rec, pcm_format_t f)
|
---|
| 142 | {
|
---|
| 143 | assert(rec);
|
---|
| 144 | assert(rec->device);
|
---|
[b7fd2a0] | 145 | errno_t ret = audio_pcm_register_event_callback(rec->device,
|
---|
[0a4ad7d] | 146 | device_event_callback, rec);
|
---|
| 147 | if (ret != EOK) {
|
---|
| 148 | printf("Failed to register for events: %s.\n", str_error(ret));
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 | rec->buffer.position = rec->buffer.base;
|
---|
| 152 | printf("Recording: %dHz, %s, %d channel(s).\n", f.sampling_rate,
|
---|
| 153 | pcm_sample_format_str(f.sample_format), f.channels);
|
---|
| 154 | const unsigned frames =
|
---|
[3bacee1] | 155 | pcm_format_size_to_frames(rec->buffer.size / BUFFER_PARTS, &f);
|
---|
[0a4ad7d] | 156 | ret = audio_pcm_start_capture_fragment(rec->device,
|
---|
| 157 | frames, f.channels, f.sampling_rate, f.sample_format);
|
---|
| 158 | if (ret != EOK) {
|
---|
| 159 | printf("Failed to start recording: %s.\n", str_error(ret));
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | getchar();
|
---|
| 164 | printf("\n");
|
---|
| 165 | audio_pcm_stop_capture(rec->device);
|
---|
[0e4c5f0] | 166 | /* XXX Control returns even before we can be sure callbacks finished */
|
---|
| 167 | printf("Delay before playback termination\n");
|
---|
[5f97ef44] | 168 | fibril_usleep(1000000);
|
---|
[0e4c5f0] | 169 | printf("Terminate playback\n");
|
---|
[0a4ad7d] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Record directly from a device to a file.
|
---|
| 174 | * @param device The device.
|
---|
| 175 | * @param file The file.
|
---|
[d5c1051] | 176 | * @return 0 on succes, non-zero on failure.
|
---|
[0a4ad7d] | 177 | */
|
---|
| 178 | int drecord(const char *device, const char *file)
|
---|
| 179 | {
|
---|
[b7fd2a0] | 180 | errno_t ret = EOK;
|
---|
[0a4ad7d] | 181 | audio_pcm_sess_t *session = NULL;
|
---|
[e172429] | 182 | sysarg_t val;
|
---|
[0a4ad7d] | 183 | if (str_cmp(device, "default") == 0) {
|
---|
| 184 | session = audio_pcm_open_default();
|
---|
| 185 | } else {
|
---|
| 186 | session = audio_pcm_open(device);
|
---|
| 187 | }
|
---|
| 188 | if (!session) {
|
---|
| 189 | printf("Failed to connect to device %s.\n", device);
|
---|
| 190 | return 1;
|
---|
| 191 | }
|
---|
| 192 | printf("Recording on device: %s.\n", device);
|
---|
[e172429] | 193 | ret = audio_pcm_query_cap(session, AUDIO_CAP_CAPTURE, &val);
|
---|
| 194 | if (ret != EOK || !val) {
|
---|
[0a4ad7d] | 195 | printf("Device %s does not support recording\n", device);
|
---|
| 196 | ret = ENOTSUP;
|
---|
| 197 | goto close_session;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[3bacee1] | 200 | char *info = NULL;
|
---|
[0a4ad7d] | 201 | ret = audio_pcm_get_info_str(session, &info);
|
---|
| 202 | if (ret != EOK) {
|
---|
| 203 | printf("Failed to get PCM info.\n");
|
---|
| 204 | goto close_session;
|
---|
| 205 | }
|
---|
| 206 | printf("Capturing on %s.\n", info);
|
---|
| 207 | free(info);
|
---|
| 208 |
|
---|
| 209 | record_t rec;
|
---|
| 210 | record_initialize(&rec, session);
|
---|
| 211 |
|
---|
| 212 | ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
|
---|
| 213 | &rec.buffer.size);
|
---|
| 214 | if (ret != EOK) {
|
---|
| 215 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
---|
| 216 | goto close_session;
|
---|
| 217 | }
|
---|
| 218 | printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
|
---|
| 219 |
|
---|
| 220 | rec.file = fopen(file, "w");
|
---|
| 221 | if (rec.file == NULL) {
|
---|
| 222 | ret = ENOENT;
|
---|
| 223 | printf("Failed to open file: %s.\n", file);
|
---|
| 224 | goto cleanup;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | wave_header_t header;
|
---|
| 228 | fseek(rec.file, sizeof(header), SEEK_SET);
|
---|
| 229 | if (ret != EOK) {
|
---|
[279f90f] | 230 | printf("Error parsing wav header\n");
|
---|
[0a4ad7d] | 231 | goto cleanup;
|
---|
| 232 | }
|
---|
[e172429] | 233 | ret = audio_pcm_query_cap(rec.device, AUDIO_CAP_INTERRUPT, &val);
|
---|
| 234 | if (ret == EOK && val)
|
---|
[0a4ad7d] | 235 | record_fragment(&rec, format);
|
---|
| 236 | else
|
---|
| 237 | printf("Recording method is not supported");
|
---|
| 238 | //TODO consider buffer position interface
|
---|
| 239 |
|
---|
| 240 | wav_init_header(&header, format, ftell(rec.file) - sizeof(header));
|
---|
| 241 | fseek(rec.file, 0, SEEK_SET);
|
---|
| 242 | fwrite(&header, sizeof(header), 1, rec.file);
|
---|
| 243 |
|
---|
| 244 | cleanup:
|
---|
| 245 | fclose(rec.file);
|
---|
[b6e481b] | 246 | as_area_destroy(rec.buffer.base);
|
---|
[0a4ad7d] | 247 | audio_pcm_release_buffer(rec.device);
|
---|
| 248 | close_session:
|
---|
| 249 | audio_pcm_close(session);
|
---|
| 250 | return ret == EOK ? 0 : 1;
|
---|
| 251 | }
|
---|
| 252 | /**
|
---|
| 253 | * @}
|
---|
| 254 | */
|
---|