1 : <?php
2 :
3 : /**
4 : * \Midi\Util\Util
5 : *
6 : * @package Midi
7 : * @subpackage Util
8 : * @copyright © 2009 Tommy Montgomery <http://phpmidiparser.com/>
9 : * @since 1.0
10 : */
11 :
12 : namespace Midi\Util;
13 :
14 : /**
15 : * Contains static utility methods
16 : *
17 : * @package Midi
18 : * @subpackage Util
19 : * @since 1.0
20 : */
21 1 : class Util {
22 :
23 : //@codeCoverageIgnoreStart
24 : /**
25 : * Constructor
26 : *
27 : * @since 1.0
28 : * @ignore
29 : */
30 0 : private function __construct() {}
31 : //@codeCoverageIgnoreEnd
32 :
33 : /**
34 : * Unpacks a binary string into an array
35 : *
36 : * @since 1.0
37 : *
38 : * @param binary $byteSequence
39 : * @param string $limit An integer or "*"
40 : * @return array A zero-indexed array
41 : */
42 : public static function unpack($byteSequence, $limit = '*') {
43 20 : return array_values(unpack('C' . $limit, $byteSequence));
44 : }
45 :
46 : /**
47 : * Packs 8-bit integers into a binary string
48 : *
49 : * @since 1.0
50 : *
51 : * @param int $ascii,... 8-bit unsigned integer
52 : * @return binary
53 : */
54 : public static function pack($ascii) {
55 12 : $packer = new \ReflectionFunction('pack');
56 12 : $args = func_get_args();
57 12 : array_unshift($args, 'C*');
58 12 : return $packer->invokeArgs($args);
59 : }
60 :
61 : /**
62 : * Gets the number of MIDI clock ticks for the given
63 : * delta binary sequence
64 : *
65 : * @since 1.0
66 : * @uses unpack()
67 : * @todo Support unsigned integers (maybe?)
68 : *
69 : * @param string $byteSequence Delta byte sequence in binary
70 : * @return int The number of ticks
71 : */
72 : public static function getTicksFromDeltaByteSequence($byteSequence) {
73 2 : $bytes = array_reverse(self::unpack($byteSequence));
74 2 : $ticks = 0;
75 2 : $shift = 0;
76 2 : foreach ($bytes as $byte) {
77 2 : $ticks |= ($byte & 0x7F) << $shift;
78 2 : $shift += 7;
79 2 : }
80 :
81 2 : return $ticks;
82 : }
83 :
84 : /**
85 : * Gets the byte sequence for the given delta value
86 : *
87 : * @since 1.0
88 : * @uses pack()
89 : *
90 : * @param int $ticks The delta value
91 : * @return string A binary string
92 : */
93 : public static function getDeltaByteSequence($ticks) {
94 8 : $byteSequence[] = self::pack($ticks & 0x7F);
95 8 : $ticks >>= 7;
96 :
97 8 : while ($ticks > 0) {
98 7 : $byteSequence[] = self::pack(($ticks & 0x7F) | 0x80);
99 7 : $ticks >>= 7;
100 7 : }
101 :
102 8 : return implode('', array_reverse($byteSequence));
103 : }
104 :
105 : /**
106 : * Converts a binary (e.g. packed) byte sequence into a hexadecimal string
107 : *
108 : * @since 1.0
109 : * @uses binaryToHexCallback()
110 : * @uses unpack()
111 : *
112 : * @param binary $byteSequence
113 : * @return array Each element is a hex string
114 : */
115 : public static function binaryToHex($byteSequence) {
116 5 : return array_map('Midi\Util\Util::binaryToHexCallback', self::unpack($byteSequence));
117 : }
118 :
119 : /**
120 : * array_map() callback from {@link binaryToHex()}
121 : *
122 : * @since 1.0
123 : * @see binaryToHex()
124 : *
125 : * @param mixed $value
126 : * @return string
127 : */
128 : private static function binaryToHexCallback($value) {
129 5 : return str_pad(strtoupper(dechex($value)), 2, '0', STR_PAD_LEFT);
130 : }
131 :
132 : }
133 :
|