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
#![no_std]
pub use crate::errors::EncodeError;
pub use crate::formats::{InputFormat, OutputFormat};
mod encoder;
pub mod errors;
pub mod formats;
pub struct LawEncoder;
impl LawEncoder {
///Encodes audio data from one format to another, writing the encoded data into the provided output buffer.
///
///#### Parameters:
///
///- `input_format`: An `InputFormat` enum specifying the format of the input data.
///- `input_data`: A slice of `u8` representing the audio data to be encoded. This data should conform to the format specified by `input_format`.
///- `output_format`: An `OutputFormat` enum specifying the desired format of the output data.
///- `output_buffer`: A mutable slice of `u8` where the encoded data will be stored. The buffer must be large enough to hold the encoded data; otherwise, an error is returned.
///#### Returns:
///- A `Result<usize, EncodeError>` indicating the outcome of the encoding operation. On success, it returns `Ok(num_bytes)`, where `num_bytes` is the number of bytes written to `output_buffer`. On failure, it returns `Err(EncodeError)`, indicating the nature of the error.
///#### Errors:
///- `EncodeError::OutputBufferTooSmall`: This error indicates that the provided `output_buffer` is not large enough to contain the encoded data. The size of the output buffer must be at least half the size of the input data, reflecting the specific encoding algorithm's requirements.
///#### Example Usage:
///```rust
///use law_encoder::{InputFormat, OutputFormat, LawEncoder};
///let input_data = vec![/* input data bytes */];
///let mut output_buffer = vec![0u8; /* appropriate size */ 12];
///let encoder = LawEncoder;
///match encoder.encode(InputFormat::BigEndian, &input_data, OutputFormat::Alaw, &mut output_buffer) {
/// Ok(num_bytes) => println!("Encoded {} bytes successfully.", num_bytes),
/// Err(e) => println!("Encoding failed: {:?}", e),
///}
///```
///
///#### Notes:
///
///- The exact size requirement for `output_buffer` may vary depending on the input and output formats. It is generally recommended to allocate the output buffer with at least half the size of the input data to accommodate the encoded data.
pub fn encode(
&self,
input_format: InputFormat,
input_data: &[u8],
output_format: OutputFormat,
output_buffer: &mut [u8],
) -> Result<usize, EncodeError> {
if output_buffer.len() < (input_data.len() / 2) {
return Err(EncodeError::OutputBufferTooSmall);
}
let num_bytes = encoder::encode(input_format, input_data, output_format, output_buffer);
Ok(num_bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn setup() -> LawEncoder {
LawEncoder {}
}
#[test]
fn num_encoded_bytes() {
let encoder = setup();
let bytes: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let mut output: [u8; 5] = [0; 5];
let num_encoded = encoder
.encode(
InputFormat::BigEndian,
&bytes,
OutputFormat::Mulaw,
&mut output,
)
.unwrap();
assert_eq!(num_encoded, 5);
let empty = [0; 0];
let num_encoded = encoder
.encode(
InputFormat::BigEndian,
&empty,
OutputFormat::Mulaw,
&mut output,
)
.unwrap();
assert_eq!(num_encoded, 0);
}
#[test]
fn correct_encoded_bytes_big_endian() {
let encoder = setup();
// 1000 and -1000 divided across 1 byte segments in big endian
let bytes: [u8; 4] = [0b00000011, 0b11101000, 0b11111100, 0b00011000];
let mut output: [u8; 2] = [0; 2];
let _num_encoded = encoder
.encode(
InputFormat::BigEndian,
&bytes,
OutputFormat::Mulaw,
&mut output,
)
.unwrap();
assert_eq!(output, [206, 78]);
let _num_encoded = encoder
.encode(
InputFormat::BigEndian,
&bytes,
OutputFormat::Alaw,
&mut output,
)
.unwrap();
assert_eq!(output, [250, 122])
}
#[test]
fn correct_encoded_bytes_little_endian() {
let encoder = setup();
// 1000 and -1000 divided across 1 byte segments in little endian
let bytes: [u8; 4] = [0b11101000, 0b00000011, 0b00011000, 0b11111100];
let mut output: [u8; 2] = [0; 2];
let _num_encoded = encoder
.encode(
InputFormat::LittleEndian,
&bytes,
OutputFormat::Mulaw,
&mut output,
)
.unwrap();
assert_eq!(output, [206, 78]);
let _num_encoded = encoder
.encode(
InputFormat::LittleEndian,
&bytes,
OutputFormat::Alaw,
&mut output,
)
.unwrap();
assert_eq!(output, [250, 122]);
}
#[test]
fn output_buffer_error() {
let encoder = setup();
let bytes: [u8; 20] = [0; 20];
let mut output_buffer: [u8; 2] = [0; 2];
let result = encoder.encode(
InputFormat::BigEndian,
&bytes,
OutputFormat::Mulaw,
&mut output_buffer,
);
assert!(result.is_err(), "Expected an error for ouput buffer size");
}
#[test]
fn odd_length_input() {
let encoder = setup();
let bytes: [u8; 3] = [0b00000011, 0b11101000, 0b11111100];
let mut output: [u8; 2] = [0; 2];
let num_encoded_bytes = encoder
.encode(
InputFormat::BigEndian,
&bytes,
OutputFormat::Mulaw,
&mut output,
)
.unwrap();
assert_eq!(num_encoded_bytes, 1);
assert_eq!(output[0], 206);
assert_eq!(output[1], 0);
}
}