source: mainline/uspace/app/drec/drec.c@ b497018

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

Drop id parameter from audio_pcm interface.

Independent buffer should have separate nodes.

  • Property mode set to 100644
File size: 6.1 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 SUBBUFFERS 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 async_exch_t *device;
68} record_t;
69
70static void record_initialize(record_t *rec, async_exch_t *exch)
71{
72 assert(exch);
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 = exch;
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 / SUBBUFFERS;
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_RECORDING_DONE:
92 printf("+");
93 async_answer_0(callid, EOK);
94 break;
95 case PCM_EVENT_RECORDING_TERMINATED:
96 printf("\nRecording 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 int ret = audio_pcm_start_record(rec->device,
125 SUBBUFFERS, channels, sampling_rate, format);
126 if (ret != EOK) {
127 printf("Failed to start recording: %s.\n", str_error(ret));
128 return;
129 }
130
131 getchar();
132 printf("\n");
133 audio_pcm_stop_record(rec->device);
134}
135
136int main(int argc, char *argv[])
137{
138 const char *device = DEFAULT_DEVICE;
139 const char *file;
140 switch (argc) {
141 case 2:
142 file = argv[1];
143 break;
144 case 3:
145 device = argv[1];
146 file = argv[2];
147 break;
148 default:
149 printf("Usage: %s [device] file.\n", argv[0]);
150 return 1;
151 }
152
153 devman_handle_t pcm_handle;
154 int ret = devman_fun_get_handle(device, &pcm_handle, 0);
155 if (ret != EOK) {
156 printf("Failed to get device(%s) handle: %s.\n",
157 device, str_error(ret));
158 return 1;
159 }
160
161 async_sess_t *session = devman_device_connect(
162 EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
163 if (!session) {
164 printf("Failed to connect to device.\n");
165 return 1;
166 }
167
168 async_exch_t *exch = async_exchange_begin(session);
169 if (!exch) {
170 ret = EPARTY;
171 printf("Failed to start session exchange.\n");
172 goto close_session;
173 }
174 const char* info = NULL;
175 ret = audio_pcm_get_info_str(exch, &info);
176 if (ret != EOK) {
177 printf("Failed to get PCM info.\n");
178 goto close_session;
179 }
180 printf("Playing on %s.\n", info);
181 free(info);
182
183 record_t rec;
184 record_initialize(&rec, exch);
185
186 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
187 &rec.buffer.size, device_event_callback, &rec);
188 if (ret != EOK) {
189 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
190 goto close_session;
191 }
192 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
193 uintptr_t ptr = 0;
194 as_get_physical_mapping(rec.buffer.base, &ptr);
195 printf("buffer mapped at %x.\n", ptr);
196
197 rec.file = fopen(file, "wb");
198 if (rec.file == NULL) {
199 ret = ENOENT;
200 printf("Failed to open %s.\n", file);
201 goto cleanup;
202 }
203 wave_header_t header = {
204 .chunk_id = CHUNK_ID,
205 .format = FORMAT_STR,
206 .subchunk1_id = SUBCHUNK1_ID,
207 .subchunk1_size = PCM_SUBCHUNK1_SIZE,
208 .audio_format = FORMAT_LINEAR_PCM,
209 .channels = channels,
210 .sampling_rate = sampling_rate,
211 .sample_size = sample_size,
212 .byte_rate = sampling_rate * (sample_size / 8) * channels,
213 .block_align = (sample_size / 8) * channels,
214 .subchunk2_id = SUBCHUNK2_ID,
215 };
216 fwrite(&header, sizeof(header), 1, rec.file);
217 record(&rec, channels, sampling_rate, format);
218 fclose(rec.file);
219
220cleanup:
221 munmap(rec.buffer.base, rec.buffer.size);
222 audio_pcm_release_buffer(exch);
223close_session:
224 async_exchange_end(exch);
225 async_hangup(session);
226 return ret == EOK ? 0 : 1;
227}
228/**
229 * @}
230 */
Note: See TracBrowser for help on using the repository browser.