Skip to content

Commit 2f8f74e

Browse files
committed
Updated choc
1 parent 17e9914 commit 2f8f74e

32 files changed

+1427
-716
lines changed

include/soul/3rdParty/choc/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ The choice of content is driven by what I (and any other contributors) need for
1818

1919
Some of the trinkets that you'll find in here include:
2020

21-
- A fast, round-trip-accurate [float/double to string converter](./text/choc_FloatToString.h)
21+
- A fast, round-trip-accurate [float/double to string converter](./text/choc_FloatToString.h).
2222
- Some [type and value](./containers/choc_Value.h) classes which can represent typed values, but also build them dynamically, serialise them to a compact binary format, and also as [JSON](./text/choc_JSON.h).
23-
- Some [classes](./audio/choc_SampleBuffers.h) for managing buffers of multi-channel sample data, which can flexibly handle both owned buffers and non-owned views in either packed/interleaved or separate-channel formats
24-
- Some utility classes for handling [MIDI messages](./audio/choc_MIDI.h)
25-
- Some [UTF8](./text/choc_UTF8.h) validation and iteration classes that have been useful in compiler tokenisers
26-
- A scrappy collection of maths, text and container helpers which will grow randomly over time..
23+
- Some [audio buffer classes](./audio/choc_SampleBuffers.h) for managing blocks of multi-channel sample data. These can flexibly handle both owned buffers and non-owned views in either packed/interleaved or separate-channel formats.
24+
- Some [UTF8](./text/choc_UTF8.h) validation and iteration classes that have been useful in compiler tokenisers.
25+
- A handy [SmallVector](./containers/choc_SmallVector.h) class which offers a std::vector interface but has pre-allocated internal storage.
26+
- A [CodePrinter](./text/CodePrinter.h) class to help creating indented code listings.
27+
- Some utility classes for handling [MIDI messages](./audio/choc_MIDI.h) and [MIDI files](./.audio/choc_MIDIFile.h).
28+
- Some basic audio utilities like simple [oscillators](./audio/choc_Oscillators.h).
29+
- A [Javascript](./javascript/choc_javascript.h) interpreter (which is a clean C++ wrapper around the duktape library).
30+
- Various other maths, text and container helpers which will grow randomly over time..
2731

2832
Hopefully some people out there will find some of these things useful! If you do use any of it, please note that choc is not trying to be a "product" and is very much a background task for me. So requests for help, advice, features, PRs, bikeshedding, etc are likely to be respectfully ignored :)
2933

include/soul/3rdParty/choc/audio/choc_MIDI.h

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
/*
2-
██████ ██  ██  ██████  ██████
3-
██      ██  ██ ██    ██ ██       Clean Header-Only Classes
4-
██  ███████ ██  ██ ██  Copyright (C)2020 Julian Storer
5-
██  ██   ██ ██  ██ ██
6-
 ██████ ██  ██  ██████   ██████
7-
8-
The code in this file is provided under the terms of the ISC license:
9-
10-
Permission to use, copy, modify, and/or distribute this software for any purpose with
11-
or without fee is hereby granted, provided that the above copyright notice and this
12-
permission notice appear in all copies.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
15-
THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
16-
SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR
17-
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
18-
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
19-
OR PERFORMANCE OF THIS SOFTWARE.
20-
*/
1+
//
2+
// ██████ ██  ██  ██████  ██████
3+
// ██      ██  ██ ██    ██ ██       ** Clean Header-Only Classes **
4+
// ██  ███████ ██  ██ ██
5+
// ██  ██   ██ ██  ██ ██ https://github.com/Tracktion/choc
6+
//  ██████ ██  ██  ██████   ██████
7+
//
8+
// CHOC is (C)2021 Tracktion Corporation, and is offered under the terms of the ISC license:
9+
//
10+
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
11+
// without fee is hereby granted, provided that the above copyright notice and this permission
12+
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14+
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15+
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16+
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2118

2219
#ifndef CHOC_MIDI_HEADER_INCLUDED
2320
#define CHOC_MIDI_HEADER_INCLUDED
@@ -32,48 +29,53 @@ namespace choc::midi
3229
static constexpr float A440_frequency = 440.0f;
3330
static constexpr int A440_noteNumber = 69;
3431

35-
/** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz. */
32+
/// Converts a MIDI note (usually in the range 0-127) to a frequency in Hz.
3633
inline float noteNumberToFrequency (int note) { return A440_frequency * std::pow (2.0f, (static_cast<float> (note) - A440_noteNumber) * (1.0f / 12.0f)); }
37-
/** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz. */
34+
/// Converts a MIDI note (usually in the range 0-127) to a frequency in Hz.
3835
inline float noteNumberToFrequency (float note) { return A440_frequency * std::pow (2.0f, (note - static_cast<float> (A440_noteNumber)) * (1.0f / 12.0f)); }
39-
/** Converts a frequency in Hz to an equivalent MIDI note number. */
36+
/// Converts a frequency in Hz to an equivalent MIDI note number.
4037
inline float frequencyToNoteNumber (float frequency) { return static_cast<float> (A440_noteNumber) + (12.0f / std::log (2.0f)) * std::log (frequency * (1.0f / A440_frequency)); }
4138

42-
/** Returns the name for a MIDI controller number. */
39+
/// Returns the name for a MIDI controller number.
4340
inline std::string getControllerName (uint8_t controllerNumber);
4441

45-
/** Returns a space-separated string of hex digits in a fomrat that's appropriate for a MIDI data dump. */
42+
/// Returns a space-separated string of hex digits in a fomrat that's appropriate for a MIDI data dump.
4643
inline std::string printHexMIDIData (const uint8_t* data, size_t numBytes);
4744

4845
//==============================================================================
4946
/**
47+
A class to hold a 0-127 MIDI note number, which provides some helpful methods.
5048
*/
5149
struct NoteNumber
5250
{
53-
/** The MIDI note number, which must be in the range 0-127. */
51+
/// The MIDI note number, which must be in the range 0-127.
5452
uint8_t note;
5553

54+
/// A NoteNumber can be cast to an integer to get the raw MIDI note number.
5655
operator uint8_t() const { return note; }
5756

58-
/** Returns this note's position within an octave, 0-11, where C is 0. */
57+
/// Returns this note's position within an octave, 0-11, where C is 0.
5958
uint8_t getChromaticScaleIndex() const { return note % 12; }
6059

61-
/** Returns the note's octave number. */
60+
/// Returns the note's octave number.
6261
int getOctaveNumber (int octaveForMiddleC = 3) const { return note / 12 + (octaveForMiddleC - 5); }
6362

64-
/** Returns the note as a frequency in Hz. */
63+
/// Returns the note as a frequency in Hertz.
6564
float getFrequency() const { return noteNumberToFrequency (static_cast<int> (note)); }
6665

67-
/** Returns the note name, adding sharps and flats where necessary */
68-
const char* getName() const { return std::addressof ("C\0\0C#\0D\0\0Eb\0E\0\0F\0\0F#\0G\0\0G#\0A\0\0Bb\0B"[3 * getChromaticScaleIndex()]); }
69-
/** Returns the note name, adding sharps where necessary */
70-
const char* getNameWithSharps() const { return std::addressof ("C\0\0C#\0D\0\0D#\0E\0\0F\0\0F#\0G\0\0G#\0A\0\0A#\0B"[3 * getChromaticScaleIndex()]); }
71-
/** Returns the note name, adding flats where necessary */
72-
const char* getNameWithFlats() const { return std::addressof ("C\0\0Db\0D\0\0Eb\0E\0\0F\0\0Gb\0G\0\0Ab\0A\0\0Bb\0B"[3 * getChromaticScaleIndex()]); }
73-
/** Returns true if this is a "white" major scale note. */
74-
bool isWhiteNote() const { return (0b101010110101 & (1 << getChromaticScaleIndex())) != 0; }
75-
/** Returns the note name and octave number (using default choices for things like sharp/flat/octave number). */
76-
std::string getNameWithOctaveNumber() const { return getName() + std::to_string (getOctaveNumber()); }
66+
/// Returns the note name, adding sharps and flats where necessary.
67+
std::string_view getName() const { return std::addressof ("C\0\0C#\0D\0\0Eb\0E\0\0F\0\0F#\0G\0\0G#\0A\0\0Bb\0B"[3 * getChromaticScaleIndex()]); }
68+
/// Returns the note name, adding sharps where necessary.
69+
std::string_view getNameWithSharps() const { return std::addressof ("C\0\0C#\0D\0\0D#\0E\0\0F\0\0F#\0G\0\0G#\0A\0\0A#\0B"[3 * getChromaticScaleIndex()]); }
70+
/// Returns the note name, adding flats where necessary.
71+
std::string_view getNameWithFlats() const { return std::addressof ("C\0\0Db\0D\0\0Eb\0E\0\0F\0\0Gb\0G\0\0Ab\0A\0\0Bb\0B"[3 * getChromaticScaleIndex()]); }
72+
/// Returns the note name and octave number (using default choices for things like sharp/flat/octave number).
73+
std::string getNameWithOctaveNumber() const { return std::string (getName()) + std::to_string (getOctaveNumber()); }
74+
75+
/// Returns true if this is a natural note in the C major scale.
76+
bool isNatural() const { return (0b101010110101 & (1 << getChromaticScaleIndex())) != 0; }
77+
/// Returns true if this is an accidental note, i.e. a sharp or flat.
78+
bool isAccidental() const { return ! isNatural(); }
7779
};
7880

7981
//==============================================================================
@@ -372,6 +374,9 @@ inline const uint8_t* Message::data() const { return reinterpret_cast<const uin
372374

373375
inline uint8_t Message::operator[] (size_t i) const { CHOC_ASSERT (i < content.length()); return static_cast<uint8_t> (content[i]); }
374376

377+
static constexpr char sysexStartByte = -16; // 0xf0
378+
static constexpr char metaEventStartByte = -1; // 0xff
379+
375380
inline bool Message::isShortMessage() const
376381
{
377382
auto len = content.length();
@@ -380,12 +385,12 @@ inline bool Message::isShortMessage() const
380385
return false;
381386

382387
auto firstByte = content[0];
383-
return firstByte != (char) 0xff && firstByte != (char) 0xf0;
388+
return firstByte != sysexStartByte && firstByte != metaEventStartByte;
384389
}
385390

386-
inline bool Message::isSysex() const { return content.length() > 1 && content[0] == (char) 0xf0; }
387-
inline bool Message::isMetaEvent() const { return content.length() > 2 && content[0] == (char) 0xff; }
388-
inline bool Message::isMetaEventOfType (uint8_t type) const { return content.length() > 2 && content[1] == (char) type && content[0] == (char) 0xff; }
391+
inline bool Message::isSysex() const { return content.length() > 1 && content[0] == sysexStartByte; }
392+
inline bool Message::isMetaEvent() const { return content.length() > 2 && content[0] == metaEventStartByte; }
393+
inline bool Message::isMetaEventOfType (uint8_t type) const { return content.length() > 2 && content[1] == (char) type && content[0] == metaEventStartByte; }
389394

390395
inline ShortMessage Message::getShortMessage() const
391396
{

include/soul/3rdParty/choc/audio/choc_MIDIFile.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
/*
2-
██████ ██  ██  ██████  ██████
3-
██      ██  ██ ██    ██ ██       Clean Header-Only Classes
4-
██  ███████ ██  ██ ██  Copyright (C)2020 Julian Storer
5-
██  ██   ██ ██  ██ ██
6-
 ██████ ██  ██  ██████   ██████
7-
8-
The code in this file is provided under the terms of the ISC license:
9-
10-
Permission to use, copy, modify, and/or distribute this software for any purpose with
11-
or without fee is hereby granted, provided that the above copyright notice and this
12-
permission notice appear in all copies.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
15-
THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
16-
SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR
17-
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
18-
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
19-
OR PERFORMANCE OF THIS SOFTWARE.
20-
*/
1+
//
2+
// ██████ ██  ██  ██████  ██████
3+
// ██      ██  ██ ██    ██ ██       ** Clean Header-Only Classes **
4+
// ██  ███████ ██  ██ ██
5+
// ██  ██   ██ ██  ██ ██ https://github.com/Tracktion/choc
6+
//  ██████ ██  ██  ██████   ██████
7+
//
8+
// CHOC is (C)2021 Tracktion Corporation, and is offered under the terms of the ISC license:
9+
//
10+
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
11+
// without fee is hereby granted, provided that the above copyright notice and this permission
12+
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14+
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15+
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16+
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2118

2219
#ifndef CHOC_MIDIFILE_HEADER_INCLUDED
2320
#define CHOC_MIDIFILE_HEADER_INCLUDED
2421

22+
#include <functional>
2523
#include "choc_MIDISequence.h"
2624

2725
namespace choc::midi

include/soul/3rdParty/choc/audio/choc_MIDISequence.h

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
/*
2-
██████ ██  ██  ██████  ██████
3-
██      ██  ██ ██    ██ ██       Clean Header-Only Classes
4-
██  ███████ ██  ██ ██  Copyright (C)2020 Julian Storer
5-
██  ██   ██ ██  ██ ██
6-
 ██████ ██  ██  ██████   ██████
7-
8-
The code in this file is provided under the terms of the ISC license:
9-
10-
Permission to use, copy, modify, and/or distribute this software for any purpose with
11-
or without fee is hereby granted, provided that the above copyright notice and this
12-
permission notice appear in all copies.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
15-
THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
16-
SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR
17-
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
18-
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
19-
OR PERFORMANCE OF THIS SOFTWARE.
20-
*/
1+
//
2+
// ██████ ██  ██  ██████  ██████
3+
// ██      ██  ██ ██    ██ ██       ** Clean Header-Only Classes **
4+
// ██  ███████ ██  ██ ██
5+
// ██  ██   ██ ██  ██ ██ https://github.com/Tracktion/choc
6+
//  ██████ ██  ██  ██████   ██████
7+
//
8+
// CHOC is (C)2021 Tracktion Corporation, and is offered under the terms of the ISC license:
9+
//
10+
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
11+
// without fee is hereby granted, provided that the above copyright notice and this permission
12+
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14+
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15+
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16+
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2118

2219
#ifndef CHOC_MIDISEQUENCE_HEADER_INCLUDED
2320
#define CHOC_MIDISEQUENCE_HEADER_INCLUDED

0 commit comments

Comments
 (0)