@@ -142,12 +142,39 @@ void SourceInternalContainer::setAudio(
142
142
void SourceInternalContainer::writeAudio (AudioWriteType type, const juce::AudioSampleBuffer& buffer,
143
143
double startTime, double length, double sampleRate) {
144
144
if (this ->type == SourceType::Audio) {
145
+ /* * Limit Length */
146
+ length = std::min (length, buffer.getNumSamples () / sampleRate);
147
+
145
148
/* * Init Audio */
146
149
if (!this ->audioData ) {
147
150
this ->initAudioData (buffer.getNumChannels (), sampleRate, startTime + length);
148
151
}
149
152
150
- /* * TODO Write Data */
153
+ /* * Write Data */
154
+ int dstStartSample = startTime * this ->audioSampleRate ;
155
+ int dstSampleLength = length * this ->audioSampleRate ;
156
+ int channels = std::min (this ->audioData ->getNumChannels (), buffer.getNumChannels ());
157
+ if (this ->audioData ->getNumSamples () - dstSampleLength < dstStartSample) {
158
+ /* * Increase Audio Length */
159
+ this ->audioData ->setSize (
160
+ channels, dstStartSample + dstSampleLength,
161
+ true , true , true );
162
+ }
163
+
164
+ juce::AudioSampleBuffer resampleTemp{ channels, dstSampleLength };
165
+ vMath::resampleAudioData (resampleTemp, buffer,
166
+ 0 , 0 , resampleTemp.getNumSamples (), this ->audioSampleRate , sampleRate);
167
+
168
+ if (type == AudioWriteType::Insert) {
169
+ for (int i = 0 ; i < channels; i++) {
170
+ vMath::addAudioData (*(this ->audioData .get ()), resampleTemp, dstStartSample, 0 , i, i, dstSampleLength);
171
+ }
172
+ }
173
+ else if (type == AudioWriteType::Cover) {
174
+ for (int i = 0 ; i < channels; i++) {
175
+ vMath::copyAudioData (*(this ->audioData .get ()), resampleTemp, dstStartSample, 0 , i, i, dstSampleLength);
176
+ }
177
+ }
151
178
152
179
/* * Set Flag */
153
180
this ->changed ();
@@ -157,7 +184,7 @@ void SourceInternalContainer::writeAudio(AudioWriteType type, const juce::AudioS
157
184
void SourceInternalContainer::writeMIDI (MIDIWriteType type, const juce::MidiMessageSequence& sequence,
158
185
double startTime, double length) {
159
186
if (this ->type == SourceType::MIDI) {
160
- /* * Init Audio */
187
+ /* * Init MIDI */
161
188
if (!this ->midiData ) {
162
189
this ->initMidiData ();
163
190
}
0 commit comments