Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
infoword.hh
Go to the documentation of this file.
1#ifndef __INFO_WORD_HH
2#define __INFO_WORD_HH
3
4#include <ostream>
5
6#include <stdint.h>
7
8/**
9 * \author cpellegrino
10 */
11
12static
13inline unsigned int roundUpPow2(unsigned int n, unsigned int multiple)
14{
15 return (n + multiple - 1) & ~(multiple - 1);
16}
17
18struct __attribute__ ((__packed__)) InfoWord
19{
20 uint8_t head;
21 uint8_t SamplingRate;
22 uint32_t TimeInfo;
23
24 unsigned int ChannelMask() const
25 {
26 const static uint8_t mask = 0x60;
27 return (mask & head) >> 5;
28 }
29
30 unsigned int Amplitude() const
31 {
32 const static uint8_t mask = 0x18;
33 return (mask & head) >> 3;
34 }
35
36 bool Mark() const
37 {
38 const static uint8_t mask = 0x80;
39 return mask & head;
40 }
41
42 unsigned int amplitude() const
43 {
44 switch (Amplitude())
45 {
46 case 1: return 16;
47 case 2: return 24;
48 }
49 return 12;
50 }
51
52 unsigned int timeInfo() const
53 {
54 return ntohl(TimeInfo);
55 }
56
57 double samplingRate() const
58 {
59 const static double conv_factor = 1e6 / 128;
60 return SamplingRate * conv_factor;
61 }
62
63 unsigned int audioWordSize() const
64 {
65 unsigned int sample_size_bit = amplitude();
66
67 if (!ChannelMask()) // 0 ==> 2 channels, else only one
68 sample_size_bit <<= 1; // fast double the conteined value
69
70 const unsigned int size_bit = sample_size_bit + 8;
71
72 return roundUpPow2(size_bit, 16) >> 3; // return, in Bytes, the
73 // minimum multiple of 16
74 // bit greater then size_bit
75 }
76
77};
78
79inline
80std::ostream& operator <<(std::ostream& stream, const InfoWord& iw)
81{
82 return stream
83 << "Is an InfoWord: " << iw.Mark() << '\n'
84 << "ChannelMask: " << iw.ChannelMask() << '\n'
85 << "Amplitude: " << iw.Amplitude() << '\n'
86 << "Amplitude (human): " << iw.amplitude() << " bit" << '\n'
87 << "Sampling rate: " << (uint32_t) iw.SamplingRate << '\n'
88 << "Sampling rate (human): " << iw.samplingRate() << " Hz" << '\n'
89 << "TimeInfo: " << iw.timeInfo();
90}
91
92inline
93bool is_infoword(const void* const data)
94{
95 const static unsigned char mask = 0x80;
96 const unsigned char* const p = static_cast<const unsigned char* const>(
97 data);
98
99 return (*p & mask);
100}
101#endif
std::ostream & operator<<(std::ostream &stream, const InfoWord &iw)
Definition infoword.hh:80
bool is_infoword(const void *const data)
Definition infoword.hh:93
static unsigned int roundUpPow2(unsigned int n, unsigned int multiple)
Definition infoword.hh:13
struct __attribute__((__packed__)) InfoWord
Definition infoword.hh:18