• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FortySevenEffects / arduino_midi_library / 5585095195

pending completion
5585095195

Pull #304

github

web-flow
Merge 7e9222b46 into 2d64cc3c2
Pull Request #304: v5.1.0

34 of 34 new or added lines in 2 files covered. (100.0%)

535 of 535 relevant lines covered (100.0%)

119.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/MIDI.cpp
1
/*!
2
 *  @file       MIDI.cpp
3
 *  Project     Arduino MIDI Library
4
 *  @brief      MIDI Library for the Arduino
5
 *  @author     Francois Best
6
 *  @date       24/02/11
7
 *  @license    MIT - Copyright (c) 2015 Francois Best
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27

28
#include "MIDI.h"
29

30
// -----------------------------------------------------------------------------
31

32
BEGIN_MIDI_NAMESPACE
33

34
/*! \brief Encode System Exclusive messages.
35
 SysEx messages are encoded to guarantee transmission of data bytes higher than
36
 127 without breaking the MIDI protocol. Use this static method to convert the
37
 data you want to send.
38
 \param inData The data to encode.
39
 \param outSysEx The output buffer where to store the encoded message.
40
 \param inLength The length of the input buffer.
41
 \param inFlipHeaderBits True for Korg and other who store MSB in reverse order
42
 \return The length of the encoded output buffer.
43
 @see decodeSysEx
44
 Code inspired from Ruin & Wesen's SysEx encoder/decoder - http://ruinwesen.com
45
 */
46
unsigned encodeSysEx(const byte* inData,
6✔
47
                     byte* outSysEx,
48
                     unsigned inLength,
49
                     bool inFlipHeaderBits)
50
{
51
    unsigned outLength  = 0;     // Num bytes in output array.
6✔
52
    byte count          = 0;     // Num 7bytes in a block.
6✔
53
    outSysEx[0]         = 0;
6✔
54

55
    for (unsigned i = 0; i < inLength; ++i)
80✔
56
    {
57
        const byte data = inData[i];
74✔
58
        const byte msb  = data >> 7;
74✔
59
        const byte body = data & 0x7f;
74✔
60

61
        outSysEx[0] |= (msb << (inFlipHeaderBits ? count : (6 - count)));
74✔
62
        outSysEx[1 + count] = body;
74✔
63

64
        if (count++ == 6)
74✔
65
        {
66
            outSysEx   += 8;
6✔
67
            outLength  += 8;
6✔
68
            outSysEx[0] = 0;
6✔
69
            count       = 0;
6✔
70
        }
71
    }
72
    return outLength + count + (count != 0 ? 1 : 0);
6✔
73
}
74

75
/*! \brief Decode System Exclusive messages.
76
 SysEx messages are encoded to guarantee transmission of data bytes higher than
77
 127 without breaking the MIDI protocol. Use this static method to reassemble
78
 your received message.
79
 \param inSysEx The SysEx data received from MIDI in.
80
 \param outData The output buffer where to store the decrypted message.
81
 \param inLength The length of the input buffer.
82
 \param inFlipHeaderBits True for Korg and other who store MSB in reverse order
83
 \return The length of the output buffer.
84
 @see encodeSysEx @see getSysExArrayLength
85
 Code inspired from Ruin & Wesen's SysEx encoder/decoder - http://ruinwesen.com
86
 */
87
unsigned decodeSysEx(const byte* inSysEx,
6✔
88
                     byte* outData,
89
                     unsigned inLength,
90
                     bool inFlipHeaderBits)
91
{
92
    unsigned count  = 0;
6✔
93
    byte msbStorage = 0;
6✔
94
    byte byteIndex  = 0;
6✔
95

96
    for (unsigned i = 0; i < inLength; ++i)
92✔
97
    {
98
        if ((i % 8) == 0)
86✔
99
        {
100
            msbStorage = inSysEx[i];
12✔
101
            byteIndex  = 6;
12✔
102
        }
103
        else
104
        {
105
            const byte body     = inSysEx[i];
74✔
106
            const byte shift    = inFlipHeaderBits ? 6 - byteIndex : byteIndex;
74✔
107
            const byte msb      = byte(((msbStorage >> shift) & 1) << 7);
74✔
108
            byteIndex--;
74✔
109
            outData[count++] = msb | body;
74✔
110
        }
111
    }
112
    return count;
6✔
113
}
114

115
END_MIDI_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc