| 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 <stdio.h>
|
|---|
| 43 | #include <sys/mman.h>
|
|---|
| 44 | #include <sys/time.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "beep.h"
|
|---|
| 47 |
|
|---|
| 48 | #define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/dsp"
|
|---|
| 49 |
|
|---|
| 50 | static void play(async_exch_t *device, unsigned buffer_id,
|
|---|
| 51 | void *buffer, size_t size, const void *data, size_t data_size,
|
|---|
| 52 | unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
|
|---|
| 53 | {
|
|---|
| 54 | assert(device);
|
|---|
| 55 | const void* data_end = data + data_size;
|
|---|
| 56 | const size_t half_buf = size / 2;
|
|---|
| 57 |
|
|---|
| 58 | /* Time to play half the buffer. */
|
|---|
| 59 | const unsigned interval = 1000000 /
|
|---|
| 60 | (sampling_rate / (half_buf / (channels * (sample_size / 8))));
|
|---|
| 61 | printf("Time to play half buffer: %zu us.\n", interval);
|
|---|
| 62 | /* Initialize buffer. */
|
|---|
| 63 | memcpy(buffer, data, size);
|
|---|
| 64 | data += size;
|
|---|
| 65 |
|
|---|
| 66 | struct timeval time;
|
|---|
| 67 | gettimeofday(&time, NULL);
|
|---|
| 68 | printf("Starting playback.\n");
|
|---|
| 69 | int ret = audio_pcm_buffer_start_playback(device, buffer_id,
|
|---|
| 70 | sampling_rate, sample_size, channels, sign);
|
|---|
| 71 | if (ret != EOK) {
|
|---|
| 72 | printf("Failed to start playback: %s.\n", str_error(ret));
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 | void *buffer_place = buffer;
|
|---|
| 76 | while (data < data_end) {
|
|---|
| 77 | tv_add(&time, interval); /* Next update point */
|
|---|
| 78 | struct timeval current;
|
|---|
| 79 | gettimeofday(¤t, NULL);
|
|---|
| 80 |
|
|---|
| 81 | const suseconds_t delay = tv_sub(&time, ¤t);
|
|---|
| 82 | if (delay > 0)
|
|---|
| 83 | usleep(delay);
|
|---|
| 84 | const size_t remain = data_end - data;
|
|---|
| 85 | if (remain < half_buf) {
|
|---|
| 86 | memcpy(buffer_place, data, remain);
|
|---|
| 87 | bzero(buffer_place + remain, half_buf - remain);
|
|---|
| 88 | data += remain;
|
|---|
| 89 | } else {
|
|---|
| 90 | memcpy(buffer_place, data, half_buf);
|
|---|
| 91 | data += half_buf;
|
|---|
| 92 | }
|
|---|
| 93 | if (buffer_place == buffer) {
|
|---|
| 94 | buffer_place = buffer + half_buf;
|
|---|
| 95 | } else {
|
|---|
| 96 | buffer_place = buffer;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | printf("Stopping playback.\n");
|
|---|
| 101 | ret = audio_pcm_buffer_stop_playback(device, buffer_id);
|
|---|
| 102 | if (ret != EOK) {
|
|---|
| 103 | printf("Failed to stop playback: %s.\n", str_error(ret));
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | int main(int argc, char *argv[])
|
|---|
| 108 | {
|
|---|
| 109 | const char *device = DEFAULT_DEVICE;
|
|---|
| 110 | if (argc == 2)
|
|---|
| 111 | device = argv[1];
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | devman_handle_t pcm_handle;
|
|---|
| 115 | int ret = devman_fun_get_handle(device, &pcm_handle, 0);
|
|---|
| 116 | if (ret != EOK) {
|
|---|
| 117 | printf("Failed to get device(%s) handle: %s.\n",
|
|---|
| 118 | device, str_error(ret));
|
|---|
| 119 | return 1;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | async_sess_t *session = devman_device_connect(
|
|---|
| 123 | EXCHANGE_ATOMIC, pcm_handle, IPC_FLAG_BLOCKING);
|
|---|
| 124 | if (!session) {
|
|---|
| 125 | printf("Failed to connect to device.\n");
|
|---|
| 126 | return 1;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | async_exch_t *exch = async_exchange_begin(session);
|
|---|
| 130 | if (!exch) {
|
|---|
| 131 | printf("Failed to start session exchange.\n");
|
|---|
| 132 | async_hangup(session);
|
|---|
| 133 | return 1;
|
|---|
| 134 | }
|
|---|
| 135 | const char* info;
|
|---|
| 136 | ret = audio_pcm_buffer_get_info_str(exch, &info);
|
|---|
| 137 | if (ret != EOK) {
|
|---|
| 138 | printf("Failed to get PCM info.\n");
|
|---|
| 139 | async_exchange_end(exch);
|
|---|
| 140 | async_hangup(session);
|
|---|
| 141 | return 1;
|
|---|
| 142 | }
|
|---|
| 143 | printf("Playing on %s.\n", info);
|
|---|
| 144 | free(info);
|
|---|
| 145 |
|
|---|
| 146 | void *buffer = NULL;
|
|---|
| 147 | size_t size = 0;
|
|---|
| 148 | unsigned id = 0;
|
|---|
| 149 | ret = audio_pcm_buffer_get_buffer(exch, &buffer, &size, &id);
|
|---|
| 150 | if (ret != EOK) {
|
|---|
| 151 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
|---|
| 152 | async_exchange_end(exch);
|
|---|
| 153 | async_hangup(session);
|
|---|
| 154 | return 1;
|
|---|
| 155 | }
|
|---|
| 156 | printf("Buffer (%u): %p %zu.\n", id, buffer, size);
|
|---|
| 157 |
|
|---|
| 158 | play(exch, id, buffer, size, beep, beep_size,
|
|---|
| 159 | rate, sample_size, channels, sign);
|
|---|
| 160 |
|
|---|
| 161 | munmap(buffer, size);
|
|---|
| 162 | audio_pcm_buffer_release_buffer(exch, id);
|
|---|
| 163 | async_exchange_end(exch);
|
|---|
| 164 | async_hangup(session);
|
|---|
| 165 | return 0;
|
|---|
| 166 | }
|
|---|
| 167 | /**
|
|---|
| 168 | * @}
|
|---|
| 169 | */
|
|---|