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
|
use std::mem;
use std::time::Duration;
use super::*;
use ::byteorder::{ByteOrder, NetworkEndian};
const OBLIGATE_POLYPHONE: u32 = 0xffffffff;
pub enum Command {
KeepAlive,
Ping{data: [u8; 32]},
Quit,
Play{sec: u32, usec: u32, freq: u32, amp: f32, voice: u32},
Caps{voices: u32, tp: [u8; 4], ident: [u8; 24]},
PCM{samples: [i16; 16]},
Unknown{data: [u8; 36]},
}
impl Command {
const SIZE: usize = 36;
fn duration(&self) -> Option<Duration> {
match *self {
Command::Play{sec, usec, ..} => Some(Duration::new(sec as u64, usec * 1000)),
_ => None,
}
}
fn pitch(&self) -> Option<Pitch> {
match *self {
Command::Play{freq, ..} => Some(Pitch::Freq(freq as f32)),
_ => None,
}
}
}
impl<'a> From<&'a [u8; 36]> for Command {
fn from(packet: &'a [u8; 36]) -> Command {
let mut fields_u32: [u32; 9] = unsafe { mem::uninitialized() };
let mut fields_f32: [f32; 9] = unsafe { mem::uninitialized() };
NetworkEndian::read_u32_into(packet, &mut fields_u32);
unsafe { NetworkEndian::read_f32_into_unchecked(packet, &mut fields_f32); }
match fields_u32[0] {
0 => Command::KeepAlive,
1 => {
let mut data: [u8; 32] = unsafe { mem::uninitialized() };
data.copy_from_slice(&packet[4..]);
Command::Ping{data: data}
}
2 => Command::Quit,
3 => Command::Play{
sec: fields_u32[1],
usec: fields_u32[2],
freq: fields_u32[3],
amp: fields_f32[4],
voice: fields_u32[5],
},
4 => {
let mut tp: [u8; 4] = unsafe { mem::uninitialized() };
let mut ident: [u8; 24] = unsafe { mem::uninitialized() };
tp.copy_from_slice(&packet[8..12]);
ident.copy_from_slice(&packet[12..]);
Command::Caps{
voices: fields_u32[1],
tp: tp,
ident: ident,
}
},
5 => {
let mut samples: [i16; 16] = unsafe { mem::uninitialized() };
::byteorder::LittleEndian::read_i16_into(&packet[4..], &mut samples);
Command::PCM{samples: samples}
},
_ => {
let mut data: [u8; 36] = unsafe { mem::uninitialized() };
data.copy_from_slice(packet);
Command::Unknown{data: data}
}
}
}
}
|