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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f2096c9 was e5bc912, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Fix printf compile issues

  • Property mode set to 100644
File size: 6.5 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 <sys/mman.h>
43#include <inttypes.h>
44
45#include "wave.h"
46#include "drec.h"
47
48
49#define BUFFER_PARTS 2
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 case PCM_EVENT_FRAMES_CAPTURED:
106 printf("%" PRIun " frames\n", IPC_GET_ARG1(call));
107 async_answer_0(callid, EOK);
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
116 /* Write directly from device buffer to file */
117 const size_t bytes = fwrite(rec->buffer.position,
118 sizeof(uint8_t), buffer_part, rec->file);
119 printf("%zu ", bytes);
120 rec->buffer.position += buffer_part;
121
122 if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
123 rec->buffer.position = rec->buffer.base;
124 async_answer_0(callid, EOK);
125 }
126}
127
128/**
129 * Start fragment based recording.
130 * @param rec Recording helper structure.
131 * @param f PCM format
132 */
133static void record_fragment(record_t *rec, pcm_format_t f)
134{
135 assert(rec);
136 assert(rec->device);
137 int ret = audio_pcm_register_event_callback(rec->device,
138 device_event_callback, rec);
139 if (ret != EOK) {
140 printf("Failed to register for events: %s.\n", str_error(ret));
141 return;
142 }
143 rec->buffer.position = rec->buffer.base;
144 printf("Recording: %dHz, %s, %d channel(s).\n", f.sampling_rate,
145 pcm_sample_format_str(f.sample_format), f.channels);
146 const unsigned frames =
147 pcm_format_size_to_frames(rec->buffer.size / BUFFER_PARTS, &f);
148 ret = audio_pcm_start_capture_fragment(rec->device,
149 frames, f.channels, f.sampling_rate, f.sample_format);
150 if (ret != EOK) {
151 printf("Failed to start recording: %s.\n", str_error(ret));
152 return;
153 }
154
155 getchar();
156 printf("\n");
157 audio_pcm_stop_capture(rec->device);
158}
159
160/**
161 * Record directly from a device to a file.
162 * @param device The device.
163 * @param file The file.
164 * @return Error code.
165 */
166int drecord(const char *device, const char *file)
167{
168 int ret = EOK;
169 audio_pcm_sess_t *session = NULL;
170 if (str_cmp(device, "default") == 0) {
171 session = audio_pcm_open_default();
172 } else {
173 session = audio_pcm_open(device);
174 }
175 if (!session) {
176 printf("Failed to connect to device %s.\n", device);
177 return 1;
178 }
179 printf("Recording on device: %s.\n", device);
180 if (audio_pcm_query_cap(session, AUDIO_CAP_CAPTURE) <= 0) {
181 printf("Device %s does not support recording\n", device);
182 ret = ENOTSUP;
183 goto close_session;
184 }
185
186 const char* info = NULL;
187 ret = audio_pcm_get_info_str(session, &info);
188 if (ret != EOK) {
189 printf("Failed to get PCM info.\n");
190 goto close_session;
191 }
192 printf("Capturing on %s.\n", info);
193 free(info);
194
195 record_t rec;
196 record_initialize(&rec, session);
197
198 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
199 &rec.buffer.size);
200 if (ret != EOK) {
201 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
202 goto close_session;
203 }
204 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
205
206 rec.file = fopen(file, "w");
207 if (rec.file == NULL) {
208 ret = ENOENT;
209 printf("Failed to open file: %s.\n", file);
210 goto cleanup;
211 }
212
213 wave_header_t header;
214 fseek(rec.file, sizeof(header), SEEK_SET);
215 const char *error;
216 if (ret != EOK) {
217 printf("Error parsing wav header: %s.\n", error);
218 goto cleanup;
219 }
220 if (audio_pcm_query_cap(rec.device, AUDIO_CAP_INTERRUPT) > 0)
221 record_fragment(&rec, format);
222 else
223 printf("Recording method is not supported");
224 //TODO consider buffer position interface
225
226 wav_init_header(&header, format, ftell(rec.file) - sizeof(header));
227 fseek(rec.file, 0, SEEK_SET);
228 fwrite(&header, sizeof(header), 1, rec.file);
229
230cleanup:
231 fclose(rec.file);
232 munmap(rec.buffer.base, rec.buffer.size);
233 audio_pcm_release_buffer(rec.device);
234close_session:
235 audio_pcm_close(session);
236 return ret == EOK ? 0 : 1;
237}
238/**
239 * @}
240 */
Note: See TracBrowser for help on using the repository browser.