From 4435329b0381f226f1b3191336534c4f33a20550 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Wed, 17 Jun 2020 17:15:56 -0400 Subject: [PATCH] feat: sampler no longer quantizes to nearest semitone but can work between pitches fixes #447 --- Tone/instrument/Sampler.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Tone/instrument/Sampler.ts b/Tone/instrument/Sampler.ts index 8e0e0bc6..36f5fb9b 100644 --- a/Tone/instrument/Sampler.ts +++ b/Tone/instrument/Sampler.ts @@ -1,6 +1,6 @@ import { ToneAudioBuffer } from "../core/context/ToneAudioBuffer"; import { ToneAudioBuffers } from "../core/context/ToneAudioBuffers"; -import { intervalToFrequencyRatio } from "../core/type/Conversions"; +import { ftomf, intervalToFrequencyRatio } from "../core/type/Conversions"; import { FrequencyClass } from "../core/type/Frequency"; import { Frequency, Interval, MidiNote, NormalRange, Note, Time } from "../core/type/Units"; import { optionsFromArguments } from "../core/util/Defaults"; @@ -176,12 +176,14 @@ export class Sampler extends Instrument { notes = [notes]; } notes.forEach(note => { - const midi = new FrequencyClass(this.context, note).toMidi(); + const midiFloat = ftomf(new FrequencyClass(this.context, note).toFrequency()); + const midi = Math.round(midiFloat) as MidiNote; + const remainder = midiFloat - midi; // find the closest note pitch const difference = this._findClosest(midi); const closestNote = midi - difference; const buffer = this._buffers.get(closestNote); - const playbackRate = intervalToFrequencyRatio(difference); + const playbackRate = intervalToFrequencyRatio(difference + remainder); // play that note const source = new ToneBufferSource({ url: buffer,