Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stretch v2 #229

Merged
merged 22 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a4f8ef0
Merge pull request #191 from musikinformatik/develop
telephon Apr 8, 2020
dc71674
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos May 13, 2020
4dc19dc
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jun 5, 2020
25ce361
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jun 10, 2020
578b3f4
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jun 14, 2020
7e7d633
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jul 5, 2020
bfc4679
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jul 16, 2020
ff0bfe1
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jul 25, 2020
4af677b
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Aug 16, 2020
a6708e6
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Sep 8, 2020
70c8e17
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Nov 11, 2020
94d0ab6
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jan 8, 2021
17ff71d
Merge branch 'develop' of https://github.com/musikinformatik/SuperDir…
bgold-cosmos Jan 29, 2021
7dd3f07
Initial commit, still need to add/check on long sample support
bgold-cosmos Jan 30, 2021
6593248
small changes as requested
bgold-cosmos Jan 31, 2021
8938e12
remove outdated comments
bgold-cosmos Feb 3, 2021
455b958
syntax correction
bgold-cosmos Feb 3, 2021
e84cdae
bugfix
bgold-cosmos Feb 3, 2021
7784fb8
Update DirtEvent.sc
bgold-cosmos Mar 11, 2021
d5f813b
window size adjustment
bgold-cosmos Mar 11, 2021
c78d427
rewrites for clarity
bgold-cosmos Mar 23, 2021
26dba05
Further checking and updating description of algorithm
bgold-cosmos Aug 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions classes/DirtEvent.sc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ DirtEvent {
delta * ~legato.value
} {
unitDuration = unitDuration ? delta;
if (~timescale.notNil) {unitDuration = unitDuration * ~timescale };
telephon marked this conversation as resolved.
Show resolved Hide resolved
loop !? { unitDuration = unitDuration * loop.abs };
}
};
Expand Down
8 changes: 7 additions & 1 deletion classes/DirtSoundLibrary.sc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ DirtSoundLibrary {
^(
buffer: buffer.bufnum,
instrument: this.instrumentForBuffer(buffer),
stretchInstrument: this.stretchInstrumentForBuffer(buffer),
bufNumFrames: buffer.numFrames,
bufNumChannels: buffer.numChannels,
unitDuration: { buffer.duration * baseFreq / ~freq.value },
Expand All @@ -262,6 +263,10 @@ DirtSoundLibrary {
^format(synthName, buffer.numChannels, this.numChannels).asSymbol
}

stretchInstrumentForBuffer { |buffer|
^format("dirt_stretchsample_%_%", buffer.numChannels, this.numChannels).asSymbol
}

openFolder { |name, index = 0|
var buf, list;
list = buffers.at(name);
Expand All @@ -281,7 +286,8 @@ DirtSoundLibrary {
numChannels = n;
bufferEvents = bufferEvents.collect { |list|
list.do { |event|
event[\instrument] = this.instrumentForBuffer(event[\buffer])
event[\instrument] = this.instrumentForBuffer(event[\buffer]);
event[\stretchInstrument] = this.stretchInstrumentForBuffer(event[\buffer]);
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions synths/core-modules.scd
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ this may be refacored later.
{ |dirtEvent|
if(~diversion.value.isNil) {
if(~buffer.notNil) {
// argumets could be omitted using getMsgFunc, but for making it easier to understand, we write them out
dirtEvent.sendSynth(~instrument, [
// arguments could be omitted using getMsgFunc, but for making it easier to understand, we write them out
var instrument;
var args = [
bufnum: ~buffer,
sustain: ~sustain,
speed: ~speed,
Expand All @@ -48,7 +49,17 @@ this may be refacored later.
end: ~end,
pan: ~pan,
out: ~out
])
];

if(~timescale.notNil) {
instrument = ~stretchInstrument;
args = args.add(\timescale).add(~timescale);
} {
instrument = ~instrument;
};

dirtEvent.sendSynth(instrument, args);

} {
if(~instrument.isNil) {
"module 'sound': instrument not found: %".format(~s).postln
Expand Down
54 changes: 54 additions & 0 deletions synths/core-synths.scd
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,60 @@ live coding them requires that you have your SuperDirt instance in an environmen
}, [\ir, \ir, \ir, \ir, \ir, \ir, \ir, \ir, \kr]).add; // pan can be modulated
};

// this is the time-stretching sample player, based on an overlap-add method
(1..SuperDirt.maxSampleNumChannels).do { |sampleNumChannels|

var name = format("dirt_stretchsample_%_%", sampleNumChannels, numChannels);

SynthDef(name, { |out, bufnum, sustain = 1, begin = 0, end = 1, speed = 1, endSpeed = 1, freq = 440, pan = 0, timescale = 1|

var sound, rate, phase, sawrate, numFrames, index, windowIndex, window, timescaleStep;
var sound0, sound1, windowSize;

// playback speed
rate = Line.kr(speed, endSpeed, sustain) * (freq / 60.midicps);

// sample phase
// BufSampleRate adjusts the rate if the sound file doesn't have the same rate as the soundcard
//phase = Sweep.ar(1, rate * BufSampleRate.ir(bufnum)) + (BufFrames.ir(bufnum) * begin);

numFrames = BufFrames.ir(bufnum);
// try windows that are 1/29th the width of the result, but not too small or large or it sounds weird
windowSize = numFrames * timescale / 29.0;
telephon marked this conversation as resolved.
Show resolved Hide resolved
windowSize = windowSize.clip(1000*timescale, BufSampleRate.ir(bufnum)/10);
telephon marked this conversation as resolved.
Show resolved Hide resolved
timescaleStep = windowSize / timescale;
sawrate = rate * BufSampleRate.ir(bufnum) / (absdif(begin, end) * timescale * numFrames);
telephon marked this conversation as resolved.
Show resolved Hide resolved
phase = (speed.sign * LFSaw.ar(sawrate, 1)).range(begin,end) * numFrames * timescale;
// do the overlap-add by running through a pair of indices, shifting weights with the window function
index = (phase.div(timescaleStep) - [1.0, 0.0]) * (timescaleStep / timescale - timescaleStep) + phase;
windowIndex = phase - (phase.div(timescaleStep) - [1.0, 0.0] * timescaleStep);
// Gaussian window, the "50" means it's about 3.5 sigma to the edge of the window
window = exp(-50 * squared(windowIndex/windowSize - 0.5));

sound0 = window[0] * BufRd.ar(
numChannels: sampleNumChannels,
bufnum: bufnum,
phase: index[0],
loop: 0,
interpolation: 4 // cubic interpolation
);
sound1 = window[1] * BufRd.ar(
numChannels: sampleNumChannels,
bufnum: bufnum,
phase: index[1],
loop: 0,
interpolation: 4 // cubic interpolation
);
sound = (sound0 + sound1) / window.sum;

sound = DirtPan.ar(sound, numChannels, pan);

Out.ar(out, sound)
}, [\ir, \ir, \ir, \ir, \ir, \ir, \ir, \ir, \ir, \ir]).add;
};



/*

Bus Routing Monitor
Expand Down