source: mainline/uspace/app/drec/drec.c@ 018ab50

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

audio: Move event callback into separate API calls.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2012 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_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#include <pcm/sample_format.h>
50
51#include "wave.h"
52
53#define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/pcm"
54#define BUFFER_PARTS 2
55
56const unsigned sampling_rate = 44100, channels = 2, sample_size = 16;
57const pcm_sample_format_t format = PCM_SAMPLE_SINT16_LE;
58
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
70static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
71{
72 assert(sess);
73 assert(rec);
74 rec->buffer.base = NULL;
75 rec->buffer.size = 0;
76 rec->buffer.position = NULL;
77 rec->file = NULL;
78 rec->device = sess;
79}
80
81
82static 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 / BUFFER_PARTS;
87 while (1) {
88 ipc_call_t call;
89 ipc_callid_t callid = async_get_call(&call);
90 switch(IPC_GET_IMETHOD(call)) {
91 case PCM_EVENT_FRAMES_CAPTURED:
92 printf("%u frames\n", IPC_GET_ARG1(call));
93 async_answer_0(callid, EOK);
94 break;
95 case PCM_EVENT_CAPTURE_TERMINATED:
96 printf("Recording terminated\n");
97 return;
98 default:
99 printf("Unknown event %d.\n", IPC_GET_IMETHOD(call));
100 async_answer_0(callid, ENOTSUP);
101 continue;
102
103 }
104 const size_t bytes = fwrite(rec->buffer.position,
105 sizeof(uint8_t), buffer_part, rec->file);
106 printf("%zu ", bytes);
107 rec->buffer.position += buffer_part;
108
109 if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
110 rec->buffer.position = rec->buffer.base;
111 async_answer_0(callid, EOK);
112 }
113}
114
115
116static void record(record_t *rec, unsigned channels, unsigned sampling_rate,
117 pcm_sample_format_t format)
118{
119 assert(rec);
120 assert(rec->device);
121 rec->buffer.position = rec->buffer.base;
122 printf("Recording: %dHz, %s, %d channel(s).\n",
123 sampling_rate, pcm_sample_format_str(format), channels);
124 const unsigned frames = rec->buffer.size /
125 (BUFFER_PARTS * channels * pcm_sample_format_size(format));
126 int ret = audio_pcm_start_capture(rec->device,
127 frames, channels, sampling_rate, format);
128 if (ret != EOK) {
129 printf("Failed to start recording: %s.\n", str_error(ret));
130 return;
131 }
132
133 getchar();
134 printf("\n");
135 audio_pcm_stop_capture(rec->device);
136}
137
138int main(int argc, char *argv[])
139{
140 const char *device = DEFAULT_DEVICE;
141 const char *file;
142 switch (argc) {
143 case 2:
144 file = argv[1];
145 break;
146 case 3:
147 device = argv[1];
148 file = argv[2];
149 break;
150 default:
151 printf("Usage: %s [device] file.\n", argv[0]);
152 return 1;
153 }
154
155
156 audio_pcm_sess_t *session = audio_pcm_open(device);
157 if (!session) {
158 printf("Failed to connect to device.\n");
159 return 1;
160 }
161
162 const char* info = NULL;
163 int ret = audio_pcm_get_info_str(session, &info);
164 if (ret != EOK) {
165 printf("Failed to get PCM info.\n");
166 goto close_session;
167 }
168 printf("Playing on %s.\n", info);
169 free(info);
170
171 record_t rec;
172 record_initialize(&rec, session);
173
174 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
175 &rec.buffer.size);
176 if (ret != EOK) {
177 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
178 goto close_session;
179 }
180 ret = audio_pcm_register_event_callback(rec.device,
181 device_event_callback, &rec);
182 if (ret != EOK) {
183 printf("Failed to register for events: %s.\n", str_error(ret));
184 goto cleanup;
185 }
186 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
187 uintptr_t ptr = 0;
188 as_get_physical_mapping(rec.buffer.base, &ptr);
189 printf("buffer mapped at %x.\n", ptr);
190
191 rec.file = fopen(file, "wb");
192 if (rec.file == NULL) {
193 ret = ENOENT;
194 printf("Failed to open %s.\n", file);
195 goto cleanup;
196 }
197 wave_header_t header = {
198 .chunk_id = CHUNK_ID,
199 .format = FORMAT_STR,
200 .subchunk1_id = SUBCHUNK1_ID,
201 .subchunk1_size = PCM_SUBCHUNK1_SIZE,
202 .audio_format = FORMAT_LINEAR_PCM,
203 .channels = channels,
204 .sampling_rate = sampling_rate,
205 .sample_size = sample_size,
206 .byte_rate = sampling_rate * (sample_size / 8) * channels,
207 .block_align = (sample_size / 8) * channels,
208 .subchunk2_id = SUBCHUNK2_ID,
209 };
210 fwrite(&header, sizeof(header), 1, rec.file);
211 record(&rec, channels, sampling_rate, format);
212 fclose(rec.file);
213
214cleanup:
215 munmap(rec.buffer.base, rec.buffer.size);
216 audio_pcm_release_buffer(rec.device);
217 audio_pcm_unregister_event_callback(rec.device);
218close_session:
219 async_hangup(session);
220 return ret == EOK ? 0 : 1;
221}
222/**
223 * @}
224 */
Note: See TracBrowser for help on using the repository browser.