Changeset View
Changeset View
Standalone View
Standalone View
share/examples/sound/basic.c
- This file was added.
/* | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
hselasky: Please add a standard 2-clause BSD license here. | |||||
* | |||||
* Copyright (c) 2021 Goran Mekić | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
Done Inline ActionsA small question: Do we get used to VLA now? khng: A small question: Do we get used to VLA now? | |||||
Done Inline ActionsI don't mind VLAs, but they shouldn't be unbounded. pstef: I don't mind VLAs, but they shouldn't be unbounded. | |||||
#include "ossinit.h" | |||||
Done Inline Actionsstyle nit: please don't cast malloc(). pstef: style nit: please don't cast malloc(). | |||||
int | |||||
main() | |||||
{ | |||||
config_t config = { | |||||
.device = "/dev/dsp", | |||||
.channels = -1, | |||||
.format = format, | |||||
.frag = -1, | |||||
Done Inline Actionsstyle nit: wrong indentation; should be better after indent run as suggested by hselasky. pstef: style nit: wrong indentation; should be better after indent run as suggested by hselasky. | |||||
.sample_rate = 48000, | |||||
.sample_size = sizeof(sample_t), | |||||
.buffer_info.fragments = -1, | |||||
Done Inline Actionsstyle nit: as per style(9), "forever loops are done with for's, not while's" pstef: style nit: as per style(9), "forever loops are done with for's, not while's" | |||||
.mmap = 0, | |||||
Done Inline ActionsCheck the return code from read/write and break the loop if not all data was read/written. hselasky: Check the return code from read/write and break the loop if not all data was read/written. | |||||
}; | |||||
/* Initialize device */ | |||||
oss_init(&config); | |||||
/* | |||||
* Allocate input and output buffers so that their size match | |||||
* frag_size | |||||
*/ | |||||
int ret; | |||||
int bytes = config.buffer_info.bytes; | |||||
int8_t *ibuf = malloc(bytes); | |||||
Done Inline Actionsreturn (0); hselasky: return (0); | |||||
int8_t *obuf = malloc(bytes); | |||||
sample_t *channels = malloc(bytes); | |||||
printf( | |||||
"bytes: %d, fragments: %d, fragsize: %d, fragstotal: %d, samples: %d\n", | |||||
bytes, | |||||
config.buffer_info.fragments, | |||||
config.buffer_info.fragsize, | |||||
config.buffer_info.fragstotal, | |||||
config.sample_count | |||||
); | |||||
/* Minimal engine: read input and copy it to the output */ | |||||
for (;;) { | |||||
ret = read(config.fd, ibuf, bytes); | |||||
if (ret < bytes) { | |||||
fprintf( | |||||
stderr, | |||||
"Requested %d bytes, but read %d!\n", | |||||
bytes, | |||||
ret | |||||
); | |||||
break; | |||||
} | |||||
oss_split(&config, (sample_t *)ibuf, channels); | |||||
/* All processing will happen here */ | |||||
oss_merge(&config, channels, (sample_t *)obuf); | |||||
ret = write(config.fd, obuf, bytes); | |||||
if (ret < bytes) { | |||||
fprintf( | |||||
stderr, | |||||
"Requested %d bytes, but wrote %d!\n", | |||||
bytes, | |||||
ret | |||||
); | |||||
break; | |||||
} | |||||
} | |||||
/* Cleanup */ | |||||
free(channels); | |||||
free(obuf); | |||||
free(ibuf); | |||||
close(config.fd); | |||||
return (0); | |||||
} |
Please add a standard 2-clause BSD license here.