-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaf.h
More file actions
136 lines (113 loc) · 3.02 KB
/
saf.h
File metadata and controls
136 lines (113 loc) · 3.02 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef SAF_H
#define SAF_H
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define SAF_VERSION_MAJOR 1
#define SAF_VERSION_MINOR 0
#define SAF_VERSION_PATCH 0
#define SAF_VERSION "1.0.0"
typedef struct saf_header
{
uint32_t sample_rate;
uint8_t channels;
uint8_t bit_depth;
} SAF_Header;
typedef enum saf_return_codes
{
SAF_OK = 0,
SAF_ERR_OPEN,
SAF_ERR_WRITE,
SAF_ERR_READ,
SAF_ERR_MEM,
} SAF_RetCode;
SAF_RetCode saf_write(const char *filename, SAF_Header header, const void *samples, size_t sample_count);
SAF_RetCode saf_read(const char *filename, SAF_Header *header, void **samples, size_t *sample_count);
void saf_free(void **samples);
const char *saf_strerror(SAF_RetCode code);
#ifdef SAF_IMPL
SAF_RetCode saf_write(const char *filename, SAF_Header header, const void *samples, size_t sample_count)
{
FILE *file = fopen(filename, "wb");
if (!file)
return SAF_ERR_OPEN;
uint8_t buf[6];
buf[0] = (header.sample_rate) & 0xFF;
buf[1] = (header.sample_rate >> 8) & 0xFF;
buf[2] = (header.sample_rate >> 16) & 0xFF;
buf[3] = (header.sample_rate >> 24) & 0xFF;
buf[4] = header.channels;
buf[5] = header.bit_depth;
if (fwrite(buf, 1, 6, file) < 6)
{
fclose(file);
return SAF_ERR_WRITE;
}
size_t byte_count = sample_count * header.channels * (header.bit_depth / 8);
if (fwrite(samples, 1, byte_count, file) < byte_count)
{
fclose(file);
return SAF_ERR_WRITE;
}
fclose(file);
return SAF_OK;
}
SAF_RetCode saf_read(const char *filename, SAF_Header *header, void **samples, size_t *sample_count)
{
FILE *file = fopen(filename, "rb");
if (!file)
return SAF_ERR_OPEN;
uint8_t buf[6];
if (fread(buf, 1, 6, file) < 6)
{
fclose(file);
return SAF_ERR_READ;
}
header->sample_rate = (uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
header->channels = buf[4];
header->bit_depth = buf[5];
fseek(file, 0, SEEK_END);
size_t data_bytes = (size_t)ftell(file) - 6;
fseek(file, 6, SEEK_SET);
*samples = malloc(data_bytes);
if (!*samples)
{
fclose(file);
return SAF_ERR_MEM;
}
if (fread(*samples, 1, data_bytes, file) < data_bytes)
{
free(*samples);
*samples = NULL;
fclose(file);
return SAF_ERR_READ;
}
*sample_count = data_bytes / (header->channels * (header->bit_depth / 8));
fclose(file);
return SAF_OK;
}
void saf_free(void **samples)
{
free(*samples);
*samples = NULL;
}
const char *saf_strerror(SAF_RetCode code)
{
switch (code)
{
case SAF_OK:
return "OK";
case SAF_ERR_OPEN:
return "Failed to open file";
case SAF_ERR_WRITE:
return "Failed to write file";
case SAF_ERR_READ:
return "Failed to read file";
case SAF_ERR_MEM:
return "Memory allocation failed";
default:
return "Unknown error";
}
}
#endif // SAF_IMPL
#endif // SAF_H