Skip to content

Commit 8bdb80d

Browse files
Add source write methods
1 parent 894ceaa commit 8bdb80d

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

src/audioCore/source/SourceInternalContainer.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ void SourceInternalContainer::setAudio(
139139
}
140140
}
141141

142+
void SourceInternalContainer::writeAudio(AudioWriteType type, const juce::AudioSampleBuffer& buffer,
143+
double startTime, double length, double sampleRate) {
144+
if (this->type == SourceType::Audio) {
145+
/** Init Audio */
146+
if (!this->audioData) {
147+
this->initAudioData(buffer.getNumChannels(), sampleRate, startTime + length);
148+
}
149+
150+
/** TODO Write Data */
151+
152+
/** Set Flag */
153+
this->changed();
154+
}
155+
}
156+
157+
void SourceInternalContainer::writeMIDI(MIDIWriteType type, const juce::MidiMessageSequence& sequence,
158+
double startTime, double length) {
159+
if (this->type == SourceType::MIDI) {
160+
/** Init Audio */
161+
if (!this->midiData) {
162+
this->initMidiData();
163+
}
164+
165+
/** TODO Write Data */
166+
167+
/** Set Flag */
168+
this->changed();
169+
}
170+
}
171+
142172
void SourceInternalContainer::setAudioFormat(const AudioFormat& format) {
143173
if (this->type == SourceType::Audio) {
144174
std::tie(this->format, this->metaData, this->bitsPerSample, this->quality) = format;

src/audioCore/source/SourceInternalContainer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class SourceInternalContainer {
3636
void setMIDI(const juce::MidiFile& data);
3737
void setAudio(double sampleRate, const juce::AudioSampleBuffer& data);
3838

39+
enum class AudioWriteType { Insert, Cover };
40+
enum class MIDIWriteType { NewTrack, Insert, Cover };
41+
void writeAudio(AudioWriteType type, const juce::AudioSampleBuffer& buffer,
42+
double startTime, double length, double sampleRate);
43+
void writeMIDI(MIDIWriteType type, const juce::MidiMessageSequence& sequence,
44+
double startTime, double length);
45+
3946
/** Format, MetaData, BitDepth, Quality */
4047
using AudioFormat = std::tuple<juce::String, juce::StringPairArray, int, int>;
4148
void setAudioFormat(const AudioFormat& format);

src/audioCore/source/SourceItem.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ const juce::MidiFile SourceItem::makeMIDIFile() const {
162162
return this->container->makeMIDIFile();
163163
}
164164

165+
void SourceItem::writeAudio(AudioWriteType type, const juce::AudioSampleBuffer& buffer,
166+
double startTime, double length, double sampleRate) {
167+
/** Check Type */
168+
if (this->type != SourceType::Audio) { return; }
169+
170+
/** Init or Fork */
171+
if (this->container) {
172+
this->forkIfNeed();
173+
}
174+
else {
175+
this->initAudio(juce::String{},
176+
buffer.getNumChannels(), sampleRate, startTime + length);
177+
}
178+
179+
/** Write Data */
180+
this->container->writeAudio(type, buffer, startTime, length, sampleRate);
181+
182+
/** Callback */
183+
this->invokeCallback();
184+
}
185+
186+
void SourceItem::writeMIDI(MIDIWriteType type, const juce::MidiMessageSequence& sequence,
187+
double startTime, double length) {
188+
/** Check Type */
189+
if (this->type != SourceType::MIDI) { return; }
190+
191+
/** Init or Fork */
192+
if (this->container) {
193+
this->forkIfNeed();
194+
}
195+
else {
196+
this->initMIDI(juce::String{});
197+
}
198+
199+
/** Write Data */
200+
this->container->writeMIDI(type, sequence, startTime, length);
201+
202+
/** Callback */
203+
this->invokeCallback();
204+
}
205+
165206
void SourceItem::changed() {
166207
if (!this->container) { return; }
167208
this->container->changed();

src/audioCore/source/SourceItem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class SourceItem final {
2323
const juce::MidiMessageSequence makeMIDITrack(int trackIndex) const;
2424
const juce::MidiFile makeMIDIFile() const;
2525

26+
using AudioWriteType = SourceInternalContainer::AudioWriteType;
27+
using MIDIWriteType = SourceInternalContainer::MIDIWriteType;
28+
void writeAudio(AudioWriteType type, const juce::AudioSampleBuffer& buffer,
29+
double startTime, double length, double sampleRate);
30+
void writeMIDI(MIDIWriteType type, const juce::MidiMessageSequence& sequence,
31+
double startTime, double length);
32+
2633
void changed();
2734
void saved();
2835
bool isSaved() const;

src/audioCore/source/SourceManager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ const juce::MidiFile SourceManager::makeMIDIFile(uint64_t ref) const {
177177
return {};
178178
}
179179

180+
void SourceManager::writeAudio(uint64_t ref,
181+
AudioWriteType type, const juce::AudioSampleBuffer& buffer,
182+
double startTime, double length, double sampleRate) {
183+
juce::ScopedWriteLock locker(audioLock::getSourceLock());
184+
if (auto ptr = this->getSource(ref, SourceType::Audio)) {
185+
ptr->writeAudio(type, buffer, startTime, length, sampleRate);
186+
}
187+
}
188+
189+
void SourceManager::writeMIDI(uint64_t ref,
190+
MIDIWriteType type, const juce::MidiMessageSequence& sequence,
191+
double startTime, double length) {
192+
juce::ScopedWriteLock locker(audioLock::getSourceLock());
193+
if (auto ptr = this->getSource(ref, SourceType::Audio)) {
194+
ptr->writeMIDI(type, sequence, startTime, length);
195+
}
196+
}
197+
180198
void SourceManager::prepareAudioPlay(uint64_t ref) {
181199
juce::ScopedWriteLock locker(audioLock::getSourceLock());
182200
if (auto ptr = this->getSource(ref, SourceType::Audio)) {
@@ -238,90 +256,103 @@ void SourceManager::readMIDIData(uint64_t ref, juce::MidiBuffer& buffer, double
238256
}
239257

240258
int SourceManager::getMIDINoteNum(uint64_t ref, int track) const {
259+
juce::ScopedReadLock locker(audioLock::getSourceLock());
241260
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
242261
return ptr->getMIDINoteNum(track);
243262
}
244263
return 0;
245264
}
246265

247266
int SourceManager::getMIDIPitchWheelNum(uint64_t ref, int track) const {
267+
juce::ScopedReadLock locker(audioLock::getSourceLock());
248268
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
249269
return ptr->getMIDIPitchWheelNum(track);
250270
}
251271
return 0;
252272
}
253273

254274
int SourceManager::getMIDIAfterTouchNum(uint64_t ref, int track) const {
275+
juce::ScopedReadLock locker(audioLock::getSourceLock());
255276
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
256277
return ptr->getMIDIAfterTouchNum(track);
257278
}
258279
return 0;
259280
}
260281

261282
int SourceManager::getMIDIChannelPressureNum(uint64_t ref, int track) const {
283+
juce::ScopedReadLock locker(audioLock::getSourceLock());
262284
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
263285
return ptr->getMIDIChannelPressureNum(track);
264286
}
265287
return 0;
266288
}
267289

268290
const std::set<uint8_t> SourceManager::getMIDIControllerNumbers(uint64_t ref, int track) const {
291+
juce::ScopedReadLock locker(audioLock::getSourceLock());
269292
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
270293
return ptr->getMIDIControllerNumbers(track);
271294
}
272295
return {};
273296
}
274297

275298
int SourceManager::getMIDIControllerNum(uint64_t ref, int track, uint8_t number) const {
299+
juce::ScopedReadLock locker(audioLock::getSourceLock());
276300
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
277301
return ptr->getMIDIControllerNum(track, number);
278302
}
279303
return 0;
280304
}
281305

282306
int SourceManager::getMIDIMiscNum(uint64_t ref, int track) const {
307+
juce::ScopedReadLock locker(audioLock::getSourceLock());
283308
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
284309
return ptr->getMIDIMiscNum(track);
285310
}
286311
return 0;
287312
}
288313

289314
const SourceMIDITemp::Note SourceManager::getMIDINote(uint64_t ref, int track, int index) const {
315+
juce::ScopedReadLock locker(audioLock::getSourceLock());
290316
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
291317
return ptr->getMIDINote(track, index);
292318
}
293319
return {};
294320
}
295321

296322
const SourceMIDITemp::IntParam SourceManager::getMIDIPitchWheel(uint64_t ref, int track, int index) const {
323+
juce::ScopedReadLock locker(audioLock::getSourceLock());
297324
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
298325
return ptr->getMIDIPitchWheel(track, index);
299326
}
300327
return {};
301328
}
302329

303330
const SourceMIDITemp::AfterTouch SourceManager::getMIDIAfterTouch(uint64_t ref, int track, int index) const {
331+
juce::ScopedReadLock locker(audioLock::getSourceLock());
304332
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
305333
return ptr->getMIDIAfterTouch(track, index);
306334
}
307335
return {};
308336
}
309337

310338
const SourceMIDITemp::IntParam SourceManager::getMIDIChannelPressure(uint64_t ref, int track, int index) const {
339+
juce::ScopedReadLock locker(audioLock::getSourceLock());
311340
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
312341
return ptr->getMIDIChannelPressure(track, index);
313342
}
314343
return {};
315344
}
316345

317346
const SourceMIDITemp::Controller SourceManager::getMIDIController(uint64_t ref, int track, uint8_t number, int index) const {
347+
juce::ScopedReadLock locker(audioLock::getSourceLock());
318348
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
319349
return ptr->getMIDIController(track, number, index);
320350
}
321351
return {};
322352
}
323353

324354
const SourceMIDITemp::Misc SourceManager::getMIDIMisc(uint64_t ref, int track, int index) const {
355+
juce::ScopedReadLock locker(audioLock::getSourceLock());
325356
if (auto ptr = this->getSourceFast(ref, SourceType::MIDI)) {
326357
return ptr->getMIDIMisc(track, index);
327358
}

src/audioCore/source/SourceManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class SourceManager final : private juce::DeletedAtShutdown {
3030
const juce::MidiMessageSequence makeMIDITrack(uint64_t ref, int trackIndex) const;
3131
const juce::MidiFile makeMIDIFile(uint64_t ref) const;
3232

33+
using AudioWriteType = SourceItem::AudioWriteType;
34+
using MIDIWriteType = SourceItem::MIDIWriteType;
35+
void writeAudio(uint64_t ref, AudioWriteType type, const juce::AudioSampleBuffer& buffer,
36+
double startTime, double length, double sampleRate);
37+
void writeMIDI(uint64_t ref, MIDIWriteType type, const juce::MidiMessageSequence& sequence,
38+
double startTime, double length);
39+
3340
void prepareAudioPlay(uint64_t ref);
3441
void prepareMIDIPlay(uint64_t ref);
3542

0 commit comments

Comments
 (0)