[e1b7e36] | 1 | /*
|
---|
[ce047249] | 2 | * Copyright (c) 2012 Jan Vesely
|
---|
[e1b7e36] | 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 dplay
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file PCM playback audio devices
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <str_error.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <devman.h>
|
---|
[90f05b0f] | 41 | #include <audio_pcm_iface.h>
|
---|
[e1b7e36] | 42 | #include <fibril_synch.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <sys/mman.h>
|
---|
| 45 | #include <sys/time.h>
|
---|
| 46 |
|
---|
| 47 | #include <stdio.h>
|
---|
| 48 | #include <macros.h>
|
---|
[ea6c838] | 49 | #include <pcm/sample_format.h>
|
---|
[e1b7e36] | 50 |
|
---|
| 51 | #include "wave.h"
|
---|
| 52 |
|
---|
[ce047249] | 53 | #define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/pcm"
|
---|
[57e8b3b] | 54 | #define BUFFER_PARTS 2
|
---|
[e1b7e36] | 55 |
|
---|
[346643c] | 56 | const unsigned sampling_rate = 44100, channels = 2, sample_size = 16;
|
---|
| 57 | const pcm_sample_format_t format = PCM_SAMPLE_SINT16_LE;
|
---|
[e1b7e36] | 58 |
|
---|
| 59 | typedef struct {
|
---|
| 60 | struct {
|
---|
| 61 | void *base;
|
---|
| 62 | size_t size;
|
---|
| 63 | unsigned id;
|
---|
| 64 | void* position;
|
---|
| 65 | } buffer;
|
---|
| 66 | FILE* file;
|
---|
[2cc5c835] | 67 | audio_pcm_sess_t *device;
|
---|
[e1b7e36] | 68 | } record_t;
|
---|
| 69 |
|
---|
[2cc5c835] | 70 | static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
|
---|
[e1b7e36] | 71 | {
|
---|
[2cc5c835] | 72 | assert(sess);
|
---|
[e1b7e36] | 73 | assert(rec);
|
---|
| 74 | rec->buffer.base = NULL;
|
---|
| 75 | rec->buffer.size = 0;
|
---|
| 76 | rec->buffer.position = NULL;
|
---|
| 77 | rec->file = NULL;
|
---|
[2cc5c835] | 78 | rec->device = sess;
|
---|
[e1b7e36] | 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void* arg)
|
---|
| 83 | {
|
---|
| 84 | async_answer_0(iid, EOK);
|
---|
| 85 | record_t *rec = arg;
|
---|
[57e8b3b] | 86 | const size_t buffer_part = rec->buffer.size / BUFFER_PARTS;
|
---|
[e1b7e36] | 87 | while (1) {
|
---|
| 88 | ipc_call_t call;
|
---|
| 89 | ipc_callid_t callid = async_get_call(&call);
|
---|
[1240bb9] | 90 | switch(IPC_GET_IMETHOD(call)) {
|
---|
[d86c9736] | 91 | case PCM_EVENT_FRAMES_CAPTURED:
|
---|
[57e8b3b] | 92 | printf("%u frames\n", IPC_GET_ARG1(call));
|
---|
[1240bb9] | 93 | async_answer_0(callid, EOK);
|
---|
[e1b7e36] | 94 | break;
|
---|
[d86c9736] | 95 | case PCM_EVENT_CAPTURE_TERMINATED:
|
---|
[57e8b3b] | 96 | printf("Recording terminated\n");
|
---|
[1240bb9] | 97 | return;
|
---|
| 98 | default:
|
---|
| 99 | printf("Unknown event %d.\n", IPC_GET_IMETHOD(call));
|
---|
| 100 | async_answer_0(callid, ENOTSUP);
|
---|
| 101 | continue;
|
---|
| 102 |
|
---|
[e1b7e36] | 103 | }
|
---|
| 104 | const size_t bytes = fwrite(rec->buffer.position,
|
---|
| 105 | sizeof(uint8_t), buffer_part, rec->file);
|
---|
| 106 | printf("%zu ", bytes);
|
---|
| 107 | rec->buffer.position += buffer_part;
|
---|
| 108 |
|
---|
| 109 | if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
|
---|
| 110 | rec->buffer.position = rec->buffer.base;
|
---|
| 111 | async_answer_0(callid, EOK);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
[346643c] | 116 | static void record(record_t *rec, unsigned channels, unsigned sampling_rate,
|
---|
| 117 | pcm_sample_format_t format)
|
---|
[e1b7e36] | 118 | {
|
---|
| 119 | assert(rec);
|
---|
| 120 | assert(rec->device);
|
---|
| 121 | rec->buffer.position = rec->buffer.base;
|
---|
[346643c] | 122 | printf("Recording: %dHz, %s, %d channel(s).\n",
|
---|
| 123 | sampling_rate, pcm_sample_format_str(format), channels);
|
---|
[57e8b3b] | 124 | const unsigned frames = rec->buffer.size /
|
---|
| 125 | (BUFFER_PARTS * channels * pcm_sample_format_size(format));
|
---|
[d86c9736] | 126 | int ret = audio_pcm_start_capture(rec->device,
|
---|
[57e8b3b] | 127 | frames, channels, sampling_rate, format);
|
---|
[e1b7e36] | 128 | if (ret != EOK) {
|
---|
| 129 | printf("Failed to start recording: %s.\n", str_error(ret));
|
---|
| 130 | return;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | getchar();
|
---|
| 134 | printf("\n");
|
---|
[d86c9736] | 135 | audio_pcm_stop_capture(rec->device);
|
---|
[e1b7e36] | 136 | }
|
---|
| 137 |
|
---|
| 138 | int main(int argc, char *argv[])
|
---|
| 139 | {
|
---|
| 140 | const char *device = DEFAULT_DEVICE;
|
---|
| 141 | const char *file;
|
---|
| 142 | switch (argc) {
|
---|
| 143 | case 2:
|
---|
| 144 | file = argv[1];
|
---|
| 145 | break;
|
---|
| 146 | case 3:
|
---|
| 147 | device = argv[1];
|
---|
| 148 | file = argv[2];
|
---|
| 149 | break;
|
---|
| 150 | default:
|
---|
| 151 | printf("Usage: %s [device] file.\n", argv[0]);
|
---|
| 152 | return 1;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
[2cc5c835] | 156 | audio_pcm_sess_t *session = audio_pcm_open(device);
|
---|
[e1b7e36] | 157 | if (!session) {
|
---|
| 158 | printf("Failed to connect to device.\n");
|
---|
| 159 | return 1;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | const char* info = NULL;
|
---|
[2cc5c835] | 163 | int ret = audio_pcm_get_info_str(session, &info);
|
---|
[e1b7e36] | 164 | if (ret != EOK) {
|
---|
| 165 | printf("Failed to get PCM info.\n");
|
---|
| 166 | goto close_session;
|
---|
| 167 | }
|
---|
| 168 | printf("Playing on %s.\n", info);
|
---|
| 169 | free(info);
|
---|
| 170 |
|
---|
| 171 | record_t rec;
|
---|
[2cc5c835] | 172 | record_initialize(&rec, session);
|
---|
[e1b7e36] | 173 |
|
---|
[90f05b0f] | 174 | ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
|
---|
[018ab50] | 175 | &rec.buffer.size);
|
---|
[e1b7e36] | 176 | if (ret != EOK) {
|
---|
| 177 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
---|
| 178 | goto close_session;
|
---|
| 179 | }
|
---|
[018ab50] | 180 | ret = audio_pcm_register_event_callback(rec.device,
|
---|
| 181 | device_event_callback, &rec);
|
---|
| 182 | if (ret != EOK) {
|
---|
| 183 | printf("Failed to register for events: %s.\n", str_error(ret));
|
---|
| 184 | goto cleanup;
|
---|
| 185 | }
|
---|
[b497018] | 186 | printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
|
---|
[e1b7e36] | 187 | uintptr_t ptr = 0;
|
---|
| 188 | as_get_physical_mapping(rec.buffer.base, &ptr);
|
---|
| 189 | printf("buffer mapped at %x.\n", ptr);
|
---|
| 190 |
|
---|
| 191 | rec.file = fopen(file, "wb");
|
---|
| 192 | if (rec.file == NULL) {
|
---|
| 193 | ret = ENOENT;
|
---|
| 194 | printf("Failed to open %s.\n", file);
|
---|
| 195 | goto cleanup;
|
---|
| 196 | }
|
---|
| 197 | wave_header_t header = {
|
---|
| 198 | .chunk_id = CHUNK_ID,
|
---|
| 199 | .format = FORMAT_STR,
|
---|
| 200 | .subchunk1_id = SUBCHUNK1_ID,
|
---|
| 201 | .subchunk1_size = PCM_SUBCHUNK1_SIZE,
|
---|
| 202 | .audio_format = FORMAT_LINEAR_PCM,
|
---|
| 203 | .channels = channels,
|
---|
| 204 | .sampling_rate = sampling_rate,
|
---|
| 205 | .sample_size = sample_size,
|
---|
[346643c] | 206 | .byte_rate = sampling_rate * (sample_size / 8) * channels,
|
---|
| 207 | .block_align = (sample_size / 8) * channels,
|
---|
[e1b7e36] | 208 | .subchunk2_id = SUBCHUNK2_ID,
|
---|
| 209 | };
|
---|
| 210 | fwrite(&header, sizeof(header), 1, rec.file);
|
---|
[1240bb9] | 211 | record(&rec, channels, sampling_rate, format);
|
---|
[e1b7e36] | 212 | fclose(rec.file);
|
---|
| 213 |
|
---|
| 214 | cleanup:
|
---|
| 215 | munmap(rec.buffer.base, rec.buffer.size);
|
---|
[2cc5c835] | 216 | audio_pcm_release_buffer(rec.device);
|
---|
[018ab50] | 217 | audio_pcm_unregister_event_callback(rec.device);
|
---|
[e1b7e36] | 218 | close_session:
|
---|
| 219 | async_hangup(session);
|
---|
| 220 | return ret == EOK ? 0 : 1;
|
---|
| 221 | }
|
---|
| 222 | /**
|
---|
| 223 | * @}
|
---|
| 224 | */
|
---|