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
// Copyright (c) 2023 0000001 1001100`
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy
// of the MPL was not distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
//! These functions convert between a custom 4-byte format (that we'll call `serato32` for brevity)
//! and 3-byte plaintext (both `u32`). Serato's custom format inserts a single null bit after every 7
//! payload bits, starting from the rightmost bit.
//!
//! This format is used to encode the 3-byte RGB color values (track color, cue colors) and the cue
//! positions and the `Serato Markers_` tag.
//!
//! # Binary Format Details
//!
//! ```text
//! serato32 | Byte1 | Byte2 | Byte3 | Byte4 |
//! | Nibb1 | Nibb2 | Nibb3 | Nibb4 | Nibb5 | Nibb6 | Nibb7 | Nibb8 |
//! Bits |A A A A B B B B C C C C D D D D E E E E F F F F G G G G H H H H|
//! Ignored Bits |^ ^ ^ ^ ^ ^ ^ ^ |
//! Plaintext ||||||||||| Byte1 | Byte2 | Byte3 |
//! ||||||||||| Nibb1 | Nibb2 | Nibb3 | Nibb4 | Nibb5 | Nibb6 |
//! ```
//!
//! More information can be found in the [format
//! documentation](https://github.com/Holzhaus/serato-tags/blob/master/docs/serato_markers_.md#custom-serato32-binary-format).
//!
//! ## Example
//!
//! | | Hex | Binary
//! | ---------------- | ------------- | ----------------------------------
//! | 3-byte plaintext | ` 00 00 cc` | ` 000 0000000 0000001 1001100`
//! | `serato32` value | `00 00 01 4c` | `00000000000000000000000101001100`
//! | |
//! | 3-byte plaintext | ` cc 88 00` | ` 110 0110010 0010000 0000000`
//! | `serato32` value | `06 32 10 00` | `00000110001100100001000000000000`
use Color;
use crateError;
use crateRes;
use u8;
use io;
/// Decodes value from Serato's 32-bit custom format to 24-bit plaintext.
///
/// # Example
/// ```rust
/// use triseratops::tag::serato32::{decode, encode};
///
/// assert_eq!(decode(0x00, 0x00, 0x01, 0x4C), (0x00, 0x00, 0xCC));
///
/// let (a, b, c, d) = encode(0x00, 0x00, 0xCC);
/// assert_eq!(decode(a, b, c, d), (0x00, 0x00, 0xCC));
/// ```
pub const
/// Encodes 3-byte value to to Serato's 32-bit custom format.
///
/// # Example
/// ```rust
/// use triseratops::tag::serato32::{decode, encode};
///
/// assert_eq!(encode(0x00, 0x00, 0xCC), (0x00, 0x00, 0x01, 0x4C));
///
/// let (x, y, z) = decode(0x00, 0x00, 0x01, 0x4C);
/// assert_eq!(encode(x, y, z), (0x00, 0x00, 0x01, 0x4C));
/// ```
pub const
/// Returns a 3-byte tuple decoded from the first 4 input bytes.
///
/// # Example
/// ```
/// use triseratops::tag::serato32::take;
/// use nom::Err;
/// use nom::error::{Error, ErrorKind};
///
/// assert_eq!(take(&[0x00, 0x00, 0x01, 0x4C]), Ok((&[][..], (0x00, 0x00, 0xCC))));
/// assert_eq!(take(&[0x00, 0x00, 0x01, 0x4C, 0x7F]), Ok((&[0x07F][..], (0x00, 0x00, 0xCC))));
/// assert!(take(&[0x00, 0x00, 0x01]).is_err());
/// ```
/// Returns a `Color` decoded from the first 4 input bytes.
///
/// # Example
/// ```
/// use triseratops::tag::color::Color;
/// use triseratops::tag::serato32::take_color;
/// use nom::Err;
/// use nom::error::{Error, ErrorKind};
///
/// assert_eq!(take_color(&[0x00, 0x00, 0x01, 0x4C]), Ok((&[][..], Color { red: 0x00, green: 0x00, blue: 0xCC})));
/// assert_eq!(take_color(&[0x00, 0x00, 0x01, 0x4C, 0x7F]), Ok((&[0x07F][..], Color { red: 0x00, green: 0x00, blue: 0xCC})));
/// assert!(take_color(&[0x00, 0x00, 0x01]).is_err());
/// ```
/// Returns a `u32` decoded from the first 4 input bytes.
///
/// The first 8 bits are always 0.
///
/// # Example
/// ```
/// use triseratops::tag::serato32::take_u32;
/// use nom::Err;
/// use nom::error::{Error, ErrorKind};
///
/// assert_eq!(take_u32(&[0x00, 0x00, 0x01, 0x4C]), Ok((&[][..], 0x0000CC)));
/// assert_eq!(take_u32(&[0x00, 0x00, 0x01, 0x4C, 0x7F]), Ok((&[0x07F][..], 0x0000CC)));
/// assert!(take_u32(&[0x00, 0x00, 0x01]).is_err());
/// ```