| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 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>
|
|---|
| 41 | #include <audio_pcm_buffer_iface.h>
|
|---|
| 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>
|
|---|
| 49 |
|
|---|
| 50 | #include "wave.h"
|
|---|
| 51 |
|
|---|
| 52 | #define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/dsp"
|
|---|
| 53 | #define SUBBUFFERS 2
|
|---|
| 54 |
|
|---|
| 55 | const unsigned sampling_rate = 44100, sample_size = 16, channels = 2;
|
|---|
| 56 | bool sign = true;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct {
|
|---|
| 59 | struct {
|
|---|
| 60 | void *base;
|
|---|
| 61 | size_t size;
|
|---|
| 62 | unsigned id;
|
|---|
| 63 | void* position;
|
|---|
| 64 | } buffer;
|
|---|
| 65 | FILE* file;
|
|---|
| 66 | async_exch_t *device;
|
|---|
| 67 | } record_t;
|
|---|
| 68 |
|
|---|
| 69 | static void record_initialize(record_t *rec, async_exch_t *exch)
|
|---|
| 70 | {
|
|---|
| 71 | assert(exch);
|
|---|
| 72 | assert(rec);
|
|---|
| 73 | rec->buffer.id = 0;
|
|---|
| 74 | rec->buffer.base = NULL;
|
|---|
| 75 | rec->buffer.size = 0;
|
|---|
| 76 | rec->buffer.position = NULL;
|
|---|
| 77 | rec->file = NULL;
|
|---|
| 78 | rec->device = exch;
|
|---|
| 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;
|
|---|
| 86 | const size_t buffer_part = rec->buffer.size / SUBBUFFERS;
|
|---|
| 87 | while (1) {
|
|---|
| 88 | ipc_call_t call;
|
|---|
| 89 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 90 | if (IPC_GET_IMETHOD(call) != IPC_FIRST_USER_METHOD) {
|
|---|
| 91 | printf("Unknown event.\n");
|
|---|
| 92 | break;
|
|---|
| 93 | }
|
|---|
| 94 | const size_t bytes = fwrite(rec->buffer.position,
|
|---|
| 95 | sizeof(uint8_t), buffer_part, rec->file);
|
|---|
| 96 | printf("%zu ", bytes);
|
|---|
| 97 | rec->buffer.position += buffer_part;
|
|---|
| 98 |
|
|---|
| 99 | if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
|
|---|
| 100 | rec->buffer.position = rec->buffer.base;
|
|---|
| 101 | async_answer_0(callid, EOK);
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | static void record(record_t *rec, unsigned sampling_rate, unsigned sample_size,
|
|---|
| 107 | unsigned channels, bool sign)
|
|---|
| 108 | {
|
|---|
| 109 | assert(rec);
|
|---|
| 110 | assert(rec->device);
|
|---|
| 111 | rec->buffer.position = rec->buffer.base;
|
|---|
| 112 | printf("Recording: %dHz, %d-bit %ssigned samples, %d channel(s).\n",
|
|---|
| 113 | sampling_rate, sample_size, sign ? "": "un", channels);
|
|---|
| 114 | int ret = audio_pcm_buffer_start_record(rec->device, rec->buffer.id,
|
|---|
| 115 | SUBBUFFERS, sampling_rate, sample_size, channels, sign);
|
|---|
| 116 | if (ret != EOK) {
|
|---|
| 117 | printf("Failed to start recording: %s.\n", str_error(ret));
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | getchar();
|
|---|
| 122 | printf("\n");
|
|---|
| 123 | audio_pcm_buffer_stop_record(rec->device, rec->buffer.id);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | int main(int argc, char *argv[])
|
|---|
| 127 | {
|
|---|
| 128 | const char *device = DEFAULT_DEVICE;
|
|---|
| 129 | const char *file;
|
|---|
| 130 | switch (argc) {
|
|---|
| 131 | case 2:
|
|---|
| 132 | file = argv[1];
|
|---|
| 133 | break;
|
|---|
| 134 | case 3:
|
|---|
| 135 | device = argv[1];
|
|---|
| 136 | file = argv[2];
|
|---|
| 137 | break;
|
|---|
| 138 | default:
|
|---|
| 139 | printf("Usage: %s [device] file.\n", argv[0]);
|
|---|
| 140 | return 1;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | devman_handle_t pcm_handle;
|
|---|
| 144 | int ret = devman_fun_get_handle(device, &pcm_handle, 0);
|
|---|
| 145 | if (ret != EOK) {
|
|---|
| 146 | printf("Failed to get device(%s) handle: %s.\n",
|
|---|
| 147 | device, str_error(ret));
|
|---|
| 148 | return 1;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | async_sess_t *session = devman_device_connect(
|
|---|
| 152 | EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
|
|---|
| 153 | if (!session) {
|
|---|
| 154 | printf("Failed to connect to device.\n");
|
|---|
| 155 | return 1;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | async_exch_t *exch = async_exchange_begin(session);
|
|---|
| 159 | if (!exch) {
|
|---|
| 160 | ret = EPARTY;
|
|---|
| 161 | printf("Failed to start session exchange.\n");
|
|---|
| 162 | goto close_session;
|
|---|
| 163 | }
|
|---|
| 164 | const char* info = NULL;
|
|---|
| 165 | ret = audio_pcm_buffer_get_info_str(exch, &info);
|
|---|
| 166 | if (ret != EOK) {
|
|---|
| 167 | printf("Failed to get PCM info.\n");
|
|---|
| 168 | goto close_session;
|
|---|
| 169 | }
|
|---|
| 170 | printf("Playing on %s.\n", info);
|
|---|
| 171 | free(info);
|
|---|
| 172 |
|
|---|
| 173 | record_t rec;
|
|---|
| 174 | record_initialize(&rec, exch);
|
|---|
| 175 |
|
|---|
| 176 | ret = audio_pcm_buffer_get_buffer(rec.device, &rec.buffer.base,
|
|---|
| 177 | &rec.buffer.size, &rec.buffer.id, device_event_callback, &rec);
|
|---|
| 178 | if (ret != EOK) {
|
|---|
| 179 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
|---|
| 180 | goto close_session;
|
|---|
| 181 | }
|
|---|
| 182 | printf("Buffer (%u): %p %zu.\n", rec.buffer.id, rec.buffer.base,
|
|---|
| 183 | rec.buffer.size);
|
|---|
| 184 | uintptr_t ptr = 0;
|
|---|
| 185 | as_get_physical_mapping(rec.buffer.base, &ptr);
|
|---|
| 186 | printf("buffer mapped at %x.\n", ptr);
|
|---|
| 187 |
|
|---|
| 188 | rec.file = fopen(file, "wb");
|
|---|
| 189 | if (rec.file == NULL) {
|
|---|
| 190 | ret = ENOENT;
|
|---|
| 191 | printf("Failed to open %s.\n", file);
|
|---|
| 192 | goto cleanup;
|
|---|
| 193 | }
|
|---|
| 194 | wave_header_t header = {
|
|---|
| 195 | .chunk_id = CHUNK_ID,
|
|---|
| 196 | .format = FORMAT_STR,
|
|---|
| 197 | .subchunk1_id = SUBCHUNK1_ID,
|
|---|
| 198 | .subchunk1_size = PCM_SUBCHUNK1_SIZE,
|
|---|
| 199 | .audio_format = FORMAT_LINEAR_PCM,
|
|---|
| 200 | .channels = channels,
|
|---|
| 201 | .sampling_rate = sampling_rate,
|
|---|
| 202 | .sample_size = sample_size,
|
|---|
| 203 | .byte_rate = (sampling_rate / 8) * channels,
|
|---|
| 204 | .block_align = (sampling_rate / 8) * channels,
|
|---|
| 205 | .subchunk2_id = SUBCHUNK2_ID,
|
|---|
| 206 | };
|
|---|
| 207 | fwrite(&header, sizeof(header), 1, rec.file);
|
|---|
| 208 | record(&rec, sampling_rate, sample_size, channels, sign);
|
|---|
| 209 | fclose(rec.file);
|
|---|
| 210 |
|
|---|
| 211 | cleanup:
|
|---|
| 212 | munmap(rec.buffer.base, rec.buffer.size);
|
|---|
| 213 | audio_pcm_buffer_release_buffer(exch, rec.buffer.id);
|
|---|
| 214 | close_session:
|
|---|
| 215 | async_exchange_end(exch);
|
|---|
| 216 | async_hangup(session);
|
|---|
| 217 | return ret == EOK ? 0 : 1;
|
|---|
| 218 | }
|
|---|
| 219 | /**
|
|---|
| 220 | * @}
|
|---|
| 221 | */
|
|---|