sound_wav, sound_aiff, sound_file: migrate from posix to stdio.

master
Ozkan Sezer 4 years ago committed by Ozkan Sezer
parent 666bd2d3d0
commit 908977d4df
  1. 46
      src/sound_aiff.c
  2. 26
      src/sound_file.c
  3. 56
      src/sound_wav.c

@ -7,10 +7,6 @@
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "sound.h"
@ -23,7 +19,7 @@ typedef struct {
} extended;
static int fd;
static FILE *fd;
static int channels;
static int bits;
static int swap_endian;
@ -44,17 +40,17 @@ static void ulong2extended(unsigned long in, extended *ex)
ex->mantissa[1] = 0;
}
static inline void write8(int fd, unsigned char c)
static inline void write8(FILE *f, unsigned char c)
{
write(fd, &c, 1);
fwrite(&c, 1, 1, f);
}
static void write32b(int fd, unsigned long w)
static void write32b(FILE *f, unsigned long w)
{
write8(fd, (w & 0xff000000) >> 24);
write8(fd, (w & 0x00ff0000) >> 16);
write8(fd, (w & 0x0000ff00) >> 8);
write8(fd, w & 0x000000ff);
write8(f, (w & 0xff000000) >> 24);
write8(f, (w & 0x00ff0000) >> 16);
write8(f, (w & 0x0000ff00) >> 8);
write8(f, w & 0x000000ff);
}
static int init(struct options *options)
@ -97,15 +93,15 @@ static int init(struct options *options)
}
if (strcmp(options->out_file, "-")) {
fd = open(options->out_file, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd < 0)
fd = fopen(options->out_file, "wb");
if (fd == NULL)
return -1;
} else {
fd = 1;
fd = stdout;
}
write(fd, hed, 54);
fwrite(hed, 1, 54, fd);
return 0;
}
@ -114,34 +110,38 @@ static void play(void *b, int len)
if (swap_endian && bits == 16) {
convert_endian(b, len);
}
write(fd, b, len);
fwrite(b, 1, len, fd);
size += len;
}
static void deinit(void)
{
if (size > 54) {
if (lseek(fd, 4, SEEK_SET) == 4) { /* FORM chunk size */
if (fseek(fd, 4, SEEK_SET) == 0) { /* FORM chunk size */
write32b(fd, size - 8);
}
if (lseek(fd, 22, SEEK_SET) == 22) { /* COMM frames */
if (fseek(fd, 22, SEEK_SET) == 0) { /* COMM frames */
unsigned long tmp = (size - 54) / (bits / 8) / channels;
write32b(fd, tmp);
}
if (lseek(fd, 42, SEEK_SET) == 42) { /* SSND chunk size */
if (fseek(fd, 42, SEEK_SET) == 0) { /* SSND chunk size */
write32b(fd, size - 48); /* minus header + 8 */
}
}
if (fd > 0) {
close(fd);
if (fd && fd != stdout) {
fclose(fd);
}
fd = NULL;
}
static void flush(void)
{
if (fd) {
fflush(fd);
}
}
static void onpause(void)

@ -7,18 +7,10 @@
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "sound.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
static int fd;
static FILE *fd;
static long size;
static int swap_endian;
@ -41,12 +33,11 @@ static int init(struct options *options)
}
if (strcmp(options->out_file, "-")) {
fd = open(options->out_file, O_WRONLY | O_CREAT | O_TRUNC
| O_BINARY, 0644);
if (fd < 0)
fd = fopen(options->out_file, "wb");
if (fd == NULL)
return -1;
} else {
fd = 1;
fd = stdout;
}
if (strcmp(options->out_file, "-")) {
@ -69,17 +60,24 @@ static void play(void *b, int len)
if (swap_endian) {
convert_endian(b, len);
}
write(fd, b, len);
fwrite(b, 1, len, fd);
size += len;
}
static void deinit(void)
{
free((void *)sound_file.description);
if (fd && fd != stdout) {
fclose(fd);
}
fd = NULL;
}
static void flush(void)
{
if (fd) {
fflush(fd);
}
}
static void onpause(void)

@ -7,50 +7,42 @@
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "sound.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
static int fd;
static FILE *fd;
static int format_16bit;
static int swap_endian;
static long size;
struct sound_driver sound_wav;
static void write_16l(int fd, unsigned short v)
static void write_16l(FILE *f, unsigned short v)
{
unsigned char x;
x = v & 0xff;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
x = v >> 8;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
}
static void write_32l(int fd, unsigned int v)
static void write_32l(FILE *f, unsigned int v)
{
unsigned char x;
x = v & 0xff;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
x = (v >> 8) & 0xff;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
x = (v >> 16) & 0xff;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
x = (v >> 24) & 0xff;
write(fd, &x, 1);
fwrite(&x, 1, 1, f);
}
static int init(struct options *options)
@ -68,12 +60,11 @@ static int init(struct options *options)
}
if (strcmp(options->out_file, "-")) {
fd = open(options->out_file, O_WRONLY | O_CREAT | O_TRUNC
| O_BINARY, 0644);
if (fd < 0)
fd = fopen(options->out_file, "wb");
if (fd == NULL)
return -1;
} else {
fd = 1;
fd = stdout;
}
if (strcmp(options->out_file, "-")) {
@ -89,9 +80,9 @@ static int init(struct options *options)
len = -1;
}
write(fd, "RIFF", 4);
fwrite("RIFF", 1, 4, fd);
write_32l(fd, len);
write(fd, "WAVE", 4);
fwrite("WAVE", 1, 4, fd);
chan = options->format & XMP_FORMAT_MONO ? 1 : 2;
sampling_rate = options->rate;
@ -108,7 +99,7 @@ static int init(struct options *options)
bytes_per_frame = chan * bits_per_sample / 8;
bytes_per_second = sampling_rate * bytes_per_frame;
write(fd, "fmt ", 4);
fwrite("fmt ", 1, 4, fd);
write_32l(fd, 16);
write_16l(fd, 1);
write_16l(fd, chan);
@ -117,7 +108,7 @@ static int init(struct options *options)
write_16l(fd, bytes_per_frame);
write_16l(fd, bits_per_sample);
write(fd, "data", 4);
fwrite("data", 1, 4, fd);
write_32l(fd, len);
size = 0;
@ -130,29 +121,32 @@ static void play(void *b, int len)
if (swap_endian && format_16bit) {
convert_endian(b, len);
}
write(fd, b, len);
fwrite(b, 1, len, fd);
size += len;
}
static void deinit(void)
{
if (lseek(fd, 40, SEEK_SET) == 40) {
if (fseek(fd, 40, SEEK_SET) == 0) {
write_32l(fd, size);
}
if (lseek(fd, 4, SEEK_SET) == 4) {
if (fseek(fd, 4, SEEK_SET) == 0) {
write_32l(fd, size + 40);
}
if (fd > 0) {
close(fd);
if (fd && fd != stdout) {
fclose(fd);
}
fd = NULL;
free((void *)sound_wav.description);
}
static void flush(void)
{
if (fd) {
fflush(fd);
}
}
static void onpause(void)

Loading…
Cancel
Save