source: mainline/uspace/app/wavplay/drec.c@ b10a434

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b10a434 was 0e4c5f0, checked in by Jiri Svoboda <jiri@…>, 10 years ago

hdaudio capture support. wavplay fixes to recording 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 if (str_cmp(device, "default") == 0) {
179 session = audio_pcm_open_default();
180 } else {
181 session = audio_pcm_open(device);
182 }
183 if (!session) {
184 printf("Failed to connect to device %s.\n", device);
185 return 1;
186 }
187 printf("Recording on device: %s.\n", device);
188 if (audio_pcm_query_cap(session, AUDIO_CAP_CAPTURE) <= 0) {
189 printf("Device %s does not support recording\n", device);
190 ret = ENOTSUP;
191 goto close_session;
192 }
193
194 const char* info = NULL;
195 ret = audio_pcm_get_info_str(session, &info);
196 if (ret != EOK) {
197 printf("Failed to get PCM info.\n");
198 goto close_session;
199 }
200 printf("Capturing on %s.\n", info);
201 free(info);
202
203 record_t rec;
204 record_initialize(&rec, session);
205
206 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
207 &rec.buffer.size);
208 if (ret != EOK) {
209 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
210 goto close_session;
211 }
212 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
213
214 rec.file = fopen(file, "w");
215 if (rec.file == NULL) {
216 ret = ENOENT;
217 printf("Failed to open file: %s.\n", file);
218 goto cleanup;
219 }
220
221 wave_header_t header;
222 fseek(rec.file, sizeof(header), SEEK_SET);
223 if (ret != EOK) {
224 printf("Error parsing wav header\n");
225 goto cleanup;
226 }
227 if (audio_pcm_query_cap(rec.device, AUDIO_CAP_INTERRUPT) > 0)
228 record_fragment(&rec, format);
229 else
230 printf("Recording method is not supported");
231 //TODO consider buffer position interface
232
233 wav_init_header(&header, format, ftell(rec.file) - sizeof(header));
234 fseek(rec.file, 0, SEEK_SET);
235 fwrite(&header, sizeof(header), 1, rec.file);
236
237cleanup:
238 fclose(rec.file);
239 as_area_destroy(rec.buffer.base);
240 audio_pcm_release_buffer(rec.device);
241close_session:
242 audio_pcm_close(session);
243 return ret == EOK ? 0 : 1;
244}
245/**
246 * @}
247 */
Note: See TracBrowser for help on using the repository browser.