source: mainline/uspace/app/drec/drec.c@ 2cc5c835

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

Cleanup audio_pcm interface.

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[e1b7e36]1/*
[ce047249]2 * Copyright (c) 2012 Jan Vesely
[e1b7e36]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>
[90f05b0f]41#include <audio_pcm_iface.h>
[e1b7e36]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>
[346643c]49#include <pcm_sample_format.h>
[e1b7e36]50
51#include "wave.h"
52
[ce047249]53#define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/pcm"
[e1b7e36]54#define SUBBUFFERS 2
55
[346643c]56const unsigned sampling_rate = 44100, channels = 2, sample_size = 16;
57const pcm_sample_format_t format = PCM_SAMPLE_SINT16_LE;
[e1b7e36]58
59typedef struct {
60 struct {
61 void *base;
62 size_t size;
63 unsigned id;
64 void* position;
65 } buffer;
66 FILE* file;
[2cc5c835]67 audio_pcm_sess_t *device;
[e1b7e36]68} record_t;
69
[2cc5c835]70static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
[e1b7e36]71{
[2cc5c835]72 assert(sess);
[e1b7e36]73 assert(rec);
74 rec->buffer.base = NULL;
75 rec->buffer.size = 0;
76 rec->buffer.position = NULL;
77 rec->file = NULL;
[2cc5c835]78 rec->device = sess;
[e1b7e36]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);
[1240bb9]90 switch(IPC_GET_IMETHOD(call)) {
91 case PCM_EVENT_RECORDING_DONE:
92 printf("+");
93 async_answer_0(callid, EOK);
[e1b7e36]94 break;
[1240bb9]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
[e1b7e36]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
[346643c]116static void record(record_t *rec, unsigned channels, unsigned sampling_rate,
117 pcm_sample_format_t format)
[e1b7e36]118{
119 assert(rec);
120 assert(rec->device);
121 rec->buffer.position = rec->buffer.base;
[346643c]122 printf("Recording: %dHz, %s, %d channel(s).\n",
123 sampling_rate, pcm_sample_format_str(format), channels);
[b497018]124 int ret = audio_pcm_start_record(rec->device,
[346643c]125 SUBBUFFERS, channels, sampling_rate, format);
[e1b7e36]126 if (ret != EOK) {
127 printf("Failed to start recording: %s.\n", str_error(ret));
128 return;
129 }
130
131 getchar();
132 printf("\n");
[b497018]133 audio_pcm_stop_record(rec->device);
[e1b7e36]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
[2cc5c835]154 audio_pcm_sess_t *session = audio_pcm_open(device);
[e1b7e36]155 if (!session) {
156 printf("Failed to connect to device.\n");
157 return 1;
158 }
159
160 const char* info = NULL;
[2cc5c835]161 int ret = audio_pcm_get_info_str(session, &info);
[e1b7e36]162 if (ret != EOK) {
163 printf("Failed to get PCM info.\n");
164 goto close_session;
165 }
166 printf("Playing on %s.\n", info);
167 free(info);
168
169 record_t rec;
[2cc5c835]170 record_initialize(&rec, session);
[e1b7e36]171
[90f05b0f]172 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
[b497018]173 &rec.buffer.size, device_event_callback, &rec);
[e1b7e36]174 if (ret != EOK) {
175 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
176 goto close_session;
177 }
[b497018]178 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
[e1b7e36]179 uintptr_t ptr = 0;
180 as_get_physical_mapping(rec.buffer.base, &ptr);
181 printf("buffer mapped at %x.\n", ptr);
182
183 rec.file = fopen(file, "wb");
184 if (rec.file == NULL) {
185 ret = ENOENT;
186 printf("Failed to open %s.\n", file);
187 goto cleanup;
188 }
189 wave_header_t header = {
190 .chunk_id = CHUNK_ID,
191 .format = FORMAT_STR,
192 .subchunk1_id = SUBCHUNK1_ID,
193 .subchunk1_size = PCM_SUBCHUNK1_SIZE,
194 .audio_format = FORMAT_LINEAR_PCM,
195 .channels = channels,
196 .sampling_rate = sampling_rate,
197 .sample_size = sample_size,
[346643c]198 .byte_rate = sampling_rate * (sample_size / 8) * channels,
199 .block_align = (sample_size / 8) * channels,
[e1b7e36]200 .subchunk2_id = SUBCHUNK2_ID,
201 };
202 fwrite(&header, sizeof(header), 1, rec.file);
[1240bb9]203 record(&rec, channels, sampling_rate, format);
[e1b7e36]204 fclose(rec.file);
205
206cleanup:
207 munmap(rec.buffer.base, rec.buffer.size);
[2cc5c835]208 audio_pcm_release_buffer(rec.device);
[e1b7e36]209close_session:
210 async_hangup(session);
211 return ret == EOK ? 0 : 1;
212}
213/**
214 * @}
215 */
Note: See TracBrowser for help on using the repository browser.