-
Notifications
You must be signed in to change notification settings - Fork 21
/
plugout_wav.c
114 lines (91 loc) · 2.22 KB
/
plugout_wav.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* gbsplay is a Gameboy sound player
*
* WAV file writer output plugin
*
* 2022 (C) by Christian Garbs <[email protected]>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
#include "util.h"
#include "filewriter.h"
#include "plugout.h"
static const uint8_t blank_hdr[44];
static long sample_rate;
static FILE* file = NULL;
static int wav_write_header() {
const uint32_t fmt_subchunk_length = 16;
const uint16_t audio_format_uncompressed_pcm = 1;
const uint16_t num_channels = 2;
const uint16_t bits_per_sample = 16;
const uint32_t byte_rate = sample_rate * num_channels * bits_per_sample / 8;
const uint16_t block_align = num_channels * bits_per_sample / 8;
long filesize = ftell(file);
if (filesize < 0 || filesize > 0xffffffff)
return -1;
fpackat(file, 0, "<{RIFF}d{WAVE}<{fmt }dwwddww{data}d",
(uint32_t)filesize - 8,
fmt_subchunk_length,
audio_format_uncompressed_pcm,
num_channels,
sample_rate,
byte_rate,
block_align,
bits_per_sample,
(uint32_t)filesize - 44);
return 0;
}
static int wav_open_file(const int subsong) {
if ((file = file_open("wav", subsong)) == NULL)
return -1;
fwrite(blank_hdr, sizeof(blank_hdr), 1, file);
return 0;
}
static int wav_close_file() {
int result;
if (wav_write_header())
return -1;
result = fclose(file);
file = NULL;
return result;
}
static long wav_open(enum plugout_endian *endian,
const long rate, long *buffer_bytes,
const struct plugout_metadata metadata)
{
UNUSED(buffer_bytes);
UNUSED(metadata);
sample_rate = rate;
*endian = PLUGOUT_ENDIAN_LITTLE;
return 0;
}
static int wav_skip(const int subsong)
{
if (file != NULL)
if (wav_close_file())
return -1;
return wav_open_file(subsong);
}
static ssize_t wav_write(const void *buf, const size_t count)
{
return fwrite(buf, count, 1, file);
}
static void wav_close()
{
if (file != NULL)
wav_close_file();
return;
}
const struct output_plugin plugout_wav = {
.name = "wav",
.description = "WAV file writer",
.open = wav_open,
.skip = wav_skip,
.write = wav_write,
.close = wav_close,
};