Skip to content

Commit 5c7ada4

Browse files
committed
Added a new class choc::json::Value, to hold and manipulate JSON values. This is almost a drop-in replacement for code that uses a choc::value::Value, but choc::value::Value was designed for some very specific use-cases involving realtime packed data handling, whereas choc::json::Value is just a simple, general purpose JSON object which allocates on the heap. That means it'll be faster and easier for most general JSON tasks.
1 parent f0f5cdf commit 5c7ada4

8 files changed

Lines changed: 2438 additions & 308 deletions

File tree

choc/audio/choc_AudioFileFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "choc_SampleBuffers.h"
2626
#include "choc_SincInterpolator.h"
2727
#include "../text/choc_StringUtilities.h"
28+
#include "../containers/choc_JSONValue.h"
2829
#include "../text/choc_JSON.h"
2930

3031
namespace choc::audio
@@ -93,7 +94,7 @@ struct AudioFileProperties
9394
/// from or written to the file. The exact format of the data in here will
9495
/// vary wildly depending on the file format. To see the names that might
9596
/// be used, have a look in the individual format classes.
96-
choc::value::Value metadata;
97+
choc::json::Value metadata;
9798

9899
std::string getDescription() const;
99100
};

choc/audio/choc_AudioFileFormat_WAV.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
358358
if (getPosition() + numLoops * 24 > chunkRange.getEnd())
359359
throwFormatException();
360360

361-
auto loops = choc::value::createEmptyArray();
361+
auto loops = choc::json::createEmptyArray();
362362

363363
for (uint32_t i = 0; i < numLoops; ++i)
364364
{
365-
auto loop = choc::value::createObject ({});
365+
auto loop = choc::json::createObject();
366366

367367
loop.setMember ("ID", static_cast<int64_t> (readInt<uint32_t>()));
368368
loop.setMember ("loopType", static_cast<int64_t> (readInt<uint32_t>()));
@@ -406,11 +406,11 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
406406
if (getPosition() + numCues * 24 > chunkRange.getEnd())
407407
throwFormatException();
408408

409-
auto cues = choc::value::createEmptyArray();
409+
auto cues = choc::json::createEmptyArray();
410410

411411
for (uint32_t i = 0; i < numCues; ++i)
412412
{
413-
auto c = choc::value::createObject ({});
413+
auto c = choc::json::createObject();
414414
c.setMember ("ID", static_cast<int64_t> (readInt<uint32_t>()));
415415
c.setMember ("position", static_cast<int64_t> (readInt<uint32_t>()));
416416
c.setMember ("dataChunkID", static_cast<int64_t> (readInt<uint32_t>()));
@@ -432,7 +432,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
432432

433433
if (subChunkType == "info" || subChunkType == "INFO")
434434
{
435-
auto listInfoItems = choc::value::createEmptyArray();
435+
auto listInfoItems = choc::json::createEmptyArray();
436436

437437
while (getPosition() < chunkRange.getEnd())
438438
{
@@ -555,10 +555,10 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
555555
return choc::base64::encodeToString (data);
556556
}
557557

558-
void addMetadata (choc::value::Value&& v)
558+
void addMetadata (choc::json::Value&& v)
559559
{
560560
if (! properties.metadata.isArray())
561-
properties.metadata = choc::value::createEmptyArray();
561+
properties.metadata = choc::json::createEmptyArray();
562562

563563
properties.metadata.addArrayElement (std::move (v));
564564
}
@@ -742,7 +742,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
742742
writeChunkNameAndSize ("data", isRF64 ? 0xffffffffu : static_cast<uint32_t> (getTotalAudioDataSize()));
743743
}
744744

745-
uint32_t getMetadataChunkSize (const choc::value::ValueView& chunk) const
745+
uint32_t getMetadataChunkSize (const choc::json::Value& chunk) const
746746
{
747747
auto type = chunk["type"].toString();
748748

@@ -761,7 +761,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
761761
return getMetadataSize_Unknown (chunk);
762762
}
763763

764-
void writeMetadataChunk (const choc::value::ValueView& chunk)
764+
void writeMetadataChunk (const choc::json::Value& chunk)
765765
{
766766
auto size = getMetadataChunkSize (chunk);
767767
auto type = chunk["type"].toString();
@@ -797,13 +797,13 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
797797
writeMetadataChunk (meta);
798798
}
799799

800-
static uint32_t getMetadataSize_BWAV (const choc::value::ValueView& bwav)
800+
static uint32_t getMetadataSize_BWAV (const choc::json::Value& bwav)
801801
{
802802
return 256 + 32 + 32 + 10 + 8 + 8 + 2 + 64 + 2 + 2 + 2 + 2 + 2 + 180
803803
+ static_cast<uint32_t> (bwav["codingHistory"].toString().length());
804804
}
805805

806-
void writeMetadata_BWAV (const choc::value::ValueView& bwav)
806+
void writeMetadata_BWAV (const choc::json::Value& bwav)
807807
{
808808
auto writeUMID = [this] (std::string_view umid)
809809
{
@@ -834,7 +834,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
834834
writeData (bwav["codingHistory"].toString());
835835
}
836836

837-
static uint32_t getMetadataSize_SMPL (const choc::value::ValueView& smpl)
837+
static uint32_t getMetadataSize_SMPL (const choc::json::Value& smpl)
838838
{
839839
const auto& loops = smpl["loops"];
840840

@@ -843,7 +843,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
843843
+ getDecodedBase64Size (smpl, "samplerData");
844844
}
845845

846-
void writeMetadata_SMPL (const choc::value::ValueView& smpl)
846+
void writeMetadata_SMPL (const choc::json::Value& smpl)
847847
{
848848
auto samplerData = getDecodedBase64 (smpl, "samplerData");
849849
const auto& loops = smpl["loops"];
@@ -879,7 +879,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
879879
return 7;
880880
}
881881

882-
void writeMetadata_INST (const choc::value::ValueView& inst)
882+
void writeMetadata_INST (const choc::json::Value& inst)
883883
{
884884
writeInt<uint8_t> (static_cast<uint8_t> (inst["baseNote"].getWithDefault<int64_t> (0)));
885885
writeInt<int8_t> (static_cast<int8_t> (inst["fineTuning"].getWithDefault<int64_t> (0)));
@@ -890,13 +890,13 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
890890
writeInt<uint8_t> (static_cast<uint8_t> (inst["highVelocity"].getWithDefault<int64_t> (0)));
891891
}
892892

893-
static uint32_t getMetadataSize_Cue (const choc::value::ValueView& cue)
893+
static uint32_t getMetadataSize_Cue (const choc::json::Value& cue)
894894
{
895895
const auto& cues = cue["cues"];
896896
return 4 + static_cast<uint32_t> (cues.isArray() ? cues.size() : 0) * 4 * 6;
897897
}
898898

899-
void writeMetadata_Cue (const choc::value::ValueView& cue)
899+
void writeMetadata_Cue (const choc::json::Value& cue)
900900
{
901901
const auto& cues = cue["cues"];
902902
writeInt<uint32_t> (static_cast<uint32_t> (cues.size()));
@@ -912,7 +912,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
912912
}
913913
}
914914

915-
static uint32_t getMetadataSize_LIST (const choc::value::ValueView& list)
915+
static uint32_t getMetadataSize_LIST (const choc::json::Value& list)
916916
{
917917
const auto& dataBase64 = list["data"];
918918

@@ -932,7 +932,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
932932
return total;
933933
}
934934

935-
void writeMetadata_LIST (const choc::value::ValueView& list)
935+
void writeMetadata_LIST (const choc::json::Value& list)
936936
{
937937
writeChunkName (list["type"].toString());
938938

@@ -965,7 +965,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
965965
return 4 + 2 + 6 + 4 + 2 + 2 + 4;
966966
}
967967

968-
void writeMetadata_ACID (const choc::value::ValueView& acid)
968+
void writeMetadata_ACID (const choc::json::Value& acid)
969969
{
970970
writeInt<uint32_t> ((acid["isOneShot"].getWithDefault<bool> (false) ? 1u : 0u)
971971
| (acid["isRootNoteSet"].getWithDefault<bool> (false) ? 2u : 0u)
@@ -980,14 +980,14 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
980980
writeInt<uint32_t> (choc::memory::bit_cast<uint32_t> (acid["tempo"].getWithDefault<float> (0)));
981981
}
982982

983-
static uint32_t getMetadataSize_Unknown (const choc::value::ValueView& u) { return 8u + getDecodedBase64Size (u, "content"); }
984-
void writeMetadata_Unknown (const choc::value::ValueView& u) { writeData (getDecodedBase64 (u, "content")); }
983+
static uint32_t getMetadataSize_Unknown (const choc::json::Value& u) { return 8u + getDecodedBase64Size (u, "content"); }
984+
void writeMetadata_Unknown (const choc::json::Value& u) { writeData (getDecodedBase64 (u, "content")); }
985985

986-
static uint32_t getMetadataSize_Trkn (const choc::value::ValueView& trkn) { return 4u + static_cast<uint32_t> (trkn["content"].toString().size()); }
987-
void writeMetadata_Trkn (const choc::value::ValueView& trkn) { writeData (trkn["content"].toString()); }
986+
static uint32_t getMetadataSize_Trkn (const choc::json::Value& trkn) { return 4u + static_cast<uint32_t> (trkn["content"].toString().size()); }
987+
void writeMetadata_Trkn (const choc::json::Value& trkn) { writeData (trkn["content"].toString()); }
988988

989-
static uint32_t getMetadataSize_AXML (const choc::value::ValueView& axml) { return 4u + static_cast<uint32_t> (axml["content"].toString().size()); }
990-
void writeMetadata_AXML (const choc::value::ValueView& axml) { writeData (axml["content"].toString()); }
989+
static uint32_t getMetadataSize_AXML (const choc::json::Value& axml) { return 4u + static_cast<uint32_t> (axml["content"].toString().size()); }
990+
void writeMetadata_AXML (const choc::json::Value& axml) { writeData (axml["content"].toString()); }
991991

992992
void writeChunkName (std::string_view name)
993993
{
@@ -1023,7 +1023,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
10231023
stream->write (s.data(), static_cast<std::streamsize> (s.size()));
10241024
}
10251025

1026-
void writePaddedString (const choc::value::ValueView& chunk, const char* name, uint32_t totalSize)
1026+
void writePaddedString (const choc::json::Value& chunk, const char* name, uint32_t totalSize)
10271027
{
10281028
auto s = chunk[name].toString();
10291029
auto sizeToWrite = std::min (totalSize, static_cast<uint32_t> (s.length()));
@@ -1033,7 +1033,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
10331033
writeZeros (totalSize - sizeToWrite);
10341034
}
10351035

1036-
static std::vector<char> getDecodedBase64 (const choc::value::ValueView& v, const char* name)
1036+
static std::vector<char> getDecodedBase64 (const choc::json::Value& v, const char* name)
10371037
{
10381038
auto asBase64 = v[name].toString();
10391039
std::vector<char> data;
@@ -1044,7 +1044,7 @@ struct WAVAudioFileFormat<supportWriting>::Implementation
10441044
return {};
10451045
}
10461046

1047-
static uint32_t getDecodedBase64Size (const choc::value::ValueView& v, const char* name)
1047+
static uint32_t getDecodedBase64Size (const choc::json::Value& v, const char* name)
10481048
{
10491049
return static_cast<uint32_t> (getDecodedBase64 (v, name).size());
10501050
}

0 commit comments

Comments
 (0)