-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathambe.go
197 lines (164 loc) · 5.17 KB
/
ambe.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package dsd
// #cgo CFLAGS: -Iinclude -I/usr/local/include -L/usr/local/lib
// #cgo LDFLAGS: -lm -lmbe -L/usr/local/lib
/*
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <mbelib.h>
#define VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT 160
typedef uint8_t flag_t;
typedef struct {
flag_t bits[72];
} dmrpacket_payload_ambe_frame_bits_t;
typedef union {
struct {
flag_t bits[108*2];
} raw;
struct {
dmrpacket_payload_ambe_frame_bits_t frames[3];
} ambe_frames;
} dmrpacket_payload_voice_bits_t;
static uint8_t voicestreams_decode_deinterleave_matrix_w[36] = {
0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x02,
0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x02, 0x00, 0x02
};
static uint8_t voicestreams_decode_deinterleave_matrix_x[36] = {
0x17, 0x0a, 0x16, 0x09, 0x15, 0x08,
0x14, 0x07, 0x13, 0x06, 0x12, 0x05,
0x11, 0x04, 0x10, 0x03, 0x0f, 0x02,
0x0e, 0x01, 0x0d, 0x00, 0x0c, 0x0a,
0x0b, 0x09, 0x0a, 0x08, 0x09, 0x07,
0x08, 0x06, 0x07, 0x05, 0x06, 0x04
};
static uint8_t voicestreams_decode_deinterleave_matrix_y[36] = {
0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x03, 0x00, 0x03,
0x01, 0x03, 0x01, 0x03, 0x01, 0x03,
0x01, 0x03, 0x01, 0x03, 0x01, 0x03,
0x01, 0x03, 0x01, 0x03, 0x01, 0x03,
0x01, 0x03, 0x01, 0x03, 0x01, 0x03
};
static uint8_t voicestreams_decode_deinterleave_matrix_z[36] = {
0x05, 0x03, 0x04, 0x02, 0x03, 0x01,
0x02, 0x00, 0x01, 0x0d, 0x00, 0x0c,
0x16, 0x0b, 0x15, 0x0a, 0x14, 0x09,
0x13, 0x08, 0x12, 0x07, 0x11, 0x06,
0x10, 0x05, 0x0f, 0x04, 0x0e, 0x03,
0x0d, 0x02, 0x0c, 0x01, 0x0b, 0x00
};
typedef struct voice_stream_s {
mbe_parms cur_mp;
mbe_parms prev_mp;
mbe_parms prev_mp_enhanced;
uint8_t decodequality;
} voicestream_t;
typedef struct {
float samples[VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT];
} voicestreams_decoded_frame_t;
voicestream_t* init_voicestream(uint8_t decodequality) {
voicestream_t *voicestream = malloc(sizeof(voicestream_t));
voicestream->decodequality = decodequality;
mbe_initMbeParms(
&voicestream->cur_mp,
&voicestream->prev_mp,
&voicestream->prev_mp_enhanced);
return voicestream;
}
void free_voicestream(voicestream_t *voicestream)
{
free(voicestream);
}
float voicestreams_decoded_frame_sample(
voicestreams_decoded_frame_t *decoded_frame,
int sample)
{
return decoded_frame->samples[sample];
}
voicestreams_decoded_frame_t *voicestreams_decode_ambe_frame(voicestream_t *voicestream, dmrpacket_payload_ambe_frame_bits_t *ambe_frame_bits) {
static voicestreams_decoded_frame_t decoded_frame;
char deinterleaved_ambe_frame_bits[4][24];
uint8_t j;
uint8_t *w, *x, *y, *z;
int errs, errs2;
char err_str[64];
char ambe_d[49];
// Deinterleaving
w = voicestreams_decode_deinterleave_matrix_w;
x = voicestreams_decode_deinterleave_matrix_x;
y = voicestreams_decode_deinterleave_matrix_y;
z = voicestreams_decode_deinterleave_matrix_z;
for (j = 0; j < sizeof(dmrpacket_payload_ambe_frame_bits_t); j += 2) {
deinterleaved_ambe_frame_bits[*w][*x] = ambe_frame_bits->bits[j];
deinterleaved_ambe_frame_bits[*y][*z] = ambe_frame_bits->bits[j+1];
w++;
x++;
y++;
z++;
}
mbe_processAmbe3600x2450Framef(decoded_frame.samples, &errs, &errs2, err_str, deinterleaved_ambe_frame_bits, ambe_d, &voicestream->cur_mp, &voicestream->prev_mp, &voicestream->prev_mp_enhanced, voicestream->decodequality);
//if (errs2 > 0)
// fprintf(stderr, "dsd/ambe: mbelib decoding errors: %u %s\n", errs2, err_str);
for (j = 0; j < VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT; j++)
decoded_frame.samples[j] /= 32767.0;
return &decoded_frame;
}
void voicestream_decode(
voicestream_t *voicestream,
char **bits,
float **samplesptr) {
int i, j;
float *samples = (float *)samplesptr;
dmrpacket_payload_voice_bits_t *voice_bits = (dmrpacket_payload_voice_bits_t *) bits;
//fprintf(stderr, "dsd/ambe: decoding %d bits\n", sizeof(voice_bits->raw.bits));
voicestreams_decoded_frame_t *decoded_frame;
for (i = 0; i < 3; i++) {
//fprintf(stderr, "dsd/ambe: decode frame %d\n", i);
decoded_frame = voicestreams_decode_ambe_frame(
voicestream,
&voice_bits->ambe_frames.frames[i]);
for (j = 0; j < VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT; j++)
samples[j+(i*VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT)] = decoded_frame->samples[j];
}
}
*/
import "C"
import (
"fmt"
"unsafe"
)
type AMBEVoiceDecodeQuality uint8
type AMBE struct {
voicestream *C.voicestream_t
}
func NewAMBE(qual AMBEVoiceDecodeQuality) *AMBE {
vs := &AMBE{}
vs.voicestream = C.init_voicestream(C.uint8_t(qual))
return vs
}
func (vs *AMBE) Decode(bits []byte) ([]float32, error) {
if len(bits) != 216 {
return nil, fmt.Errorf("need 216 bits, got %d", len(bits))
}
samples := make([]float32,
C.VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT*3,
C.VOICESTREAMS_DECODED_AMBE_FRAME_SAMPLES_COUNT*3)
C.voicestream_decode(
vs.voicestream,
(**C.char)(unsafe.Pointer(&bits[0])),
(**C.float)(unsafe.Pointer(&samples[0])))
return samples, nil
}
func (vs *AMBE) Close() error {
if vs.voicestream == nil {
return ErrClosed
}
C.free_voicestream(vs.voicestream)
vs.voicestream = nil
return nil
}
var _ (Decoder) = (*AMBE)(nil)