source: mainline/uspace/app/wavplay/drec.c@ 86bbca4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86bbca4 was e172429, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Let audio_pcm_query_cap() return value separately from error code.

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