-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsampleconv.c
149 lines (132 loc) · 3.48 KB
/
sampleconv.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* This file is part of dsp.
*
* Copyright (c) 2013-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "sampleconv.h"
void write_buf_u8(sample_t *in, void *out, ssize_t s)
{
uint8_t *outn = (uint8_t *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_U8(in[p]);
}
void read_buf_u8(void *in, sample_t *out, ssize_t s)
{
uint8_t *inn = (uint8_t *) in;
while (s-- > 0)
out[s] = U8_TO_SAMPLE(inn[s]);
}
void write_buf_s8(sample_t *in, void *out, ssize_t s)
{
int8_t *outn = (int8_t *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_S8(in[p]);
}
void read_buf_s8(void *in, sample_t *out, ssize_t s)
{
int8_t *inn = (int8_t *) in;
while (s-- > 0)
out[s] = S8_TO_SAMPLE(inn[s]);
}
void write_buf_s16(sample_t *in, void *out, ssize_t s)
{
int16_t *outn = (int16_t *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_S16(in[p]);
}
void read_buf_s16(void *in, sample_t *out, ssize_t s)
{
int16_t *inn = (int16_t *) in;
while (s-- > 0)
out[s] = S16_TO_SAMPLE(inn[s]);
}
void write_buf_s24(sample_t *in, void *out, ssize_t s)
{
int32_t *outn = (int32_t *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_S24(in[p]);
}
void read_buf_s24(void *in, sample_t *out, ssize_t s)
{
int32_t *inn = (int32_t *) in;
while (s-- > 0)
out[s] = S24_TO_SAMPLE(inn[s]);
}
void write_buf_s32(sample_t *in, void *out, ssize_t s)
{
int32_t *outn = (int32_t *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_S32(in[p]);
}
void read_buf_s32(void *in, sample_t *out, ssize_t s)
{
int32_t *inn = (int32_t *) in;
while (s-- > 0)
out[s] = S32_TO_SAMPLE(inn[s]);
}
void write_buf_s24_3(sample_t *in, void *out, ssize_t s)
{
int32_t v;
uint8_t *outn = (uint8_t *) out;
ssize_t p = -1;
while (++p < s) {
v = SAMPLE_TO_S24(in[p]);
outn[p*3 + 0] = (v >> 0) & 0xff;
outn[p*3 + 1] = (v >> 8) & 0xff;
outn[p*3 + 2] = (v >> 16) & 0xff;
}
}
void read_buf_s24_3(void *in, sample_t *out, ssize_t s)
{
int32_t v;
uint8_t *inn = (uint8_t *) in;
while (s-- > 0) {
v = (int32_t) (inn[s*3 + 0] & 0xff) << 0;
v |= (int32_t) (inn[s*3 + 1] & 0xff) << 8;
v |= (int32_t) (inn[s*3 + 2] & 0xff) << 16;
out[s] = S24_TO_SAMPLE(v);
}
}
void write_buf_float(sample_t *in, void *out, ssize_t s)
{
float *outn = (float *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_FLOAT(in[p]);
}
void read_buf_float(void *in, sample_t *out, ssize_t s)
{
float *inn = (float *) in;
while (s-- > 0)
out[s] = FLOAT_TO_SAMPLE(inn[s]);
}
void write_buf_double(sample_t *in, void *out, ssize_t s)
{
double *outn = (double *) out;
ssize_t p = -1;
while (++p < s)
outn[p] = SAMPLE_TO_DOUBLE(in[p]);
}
void read_buf_double(void *in, sample_t *out, ssize_t s)
{
double *inn = (double *) in;
while (s-- > 0)
out[s] = DOUBLE_TO_SAMPLE(inn[s]);
}