Skip to content

Commit c642f9b

Browse files
committed
Set scale from Swift
Co-authored-by: <Marc-Andre Weibezahn> [email protected]
1 parent 8359329 commit c642f9b

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

Sources/CSoundpipeAudioKit/Effects/PitchCorrectDSP.mm

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
private:
1515
sp_rms *rms_l;
1616
sp_rms *rms_r;
17+
float *scale;
18+
int scaleCount;
1719
pitchcorrect *pitchcorrect_l;
1820
pitchcorrect *pitchcorrect_r;
1921
ParameterRamper speedRamp;
@@ -24,6 +26,12 @@
2426
parameters[PitchCorrectParameterSpeed] = &speedRamp;
2527
parameters[PitchCorrectParameterAmount] = &amountRamp;
2628
}
29+
30+
void setWavetable(const float* table, size_t length, int index) override {
31+
scale = new float[length];
32+
memcpy(scale, table, length * sizeof(float));
33+
scaleCount = int(length);
34+
}
2735

2836
void init(int channelCount, double sampleRate) override {
2937
SoundpipeDSPBase::init(channelCount, sampleRate);
@@ -36,30 +44,26 @@ void init(int channelCount, double sampleRate) override {
3644
pitchcorrect_create(&pitchcorrect_r);
3745
pitchcorrect_init(sp, pitchcorrect_r);
3846

39-
// Create chromatic scale from A220 to A440
40-
float scale[13] = {
41-
220.0, // A
42-
233.08, // A#/Bb
43-
246.94, // B
44-
261.63, // C
45-
277.18, // C#/Db
46-
293.66, // D
47-
311.13, // D#/Eb
48-
329.63, // E
49-
349.23, // F
50-
369.99, // F#/Gb
51-
392.00, // G
52-
415.30, // G#/Ab
47+
// Default chromatic scale from A1 (55Hz) to A6 (1760Hz)
48+
float defaultScale[73] = {
49+
55.00, 58.27, 61.74, 65.41, 69.30, 73.42, 77.78, 82.41, 87.31, 92.50, 98.00, 103.83, // A1 to G#2
50+
110.00, 116.54, 123.47, 130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185.00, 196.00, 207.65, // A2 to G#3
51+
220.00, 233.08, 246.94, 261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, // A3 to G#4
52+
440.00, 466.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.26, 698.46, 739.99, 783.99, 830.61, // A4 to G#5
53+
880.00, 932.33, 987.77, 1046.50, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, // A5 to G#6
54+
1760.00 // A6
5355
};
54-
55-
pitchcorrect_set_scale_freqs(pitchcorrect_l, scale, 12);
56-
pitchcorrect_set_scale_freqs(pitchcorrect_r, scale, 12);
56+
setWavetable(defaultScale, 73, 0);
5757
}
5858

5959
void deinit() override {
6060
SoundpipeDSPBase::deinit();
6161
sp_rms_destroy(&rms_l);
6262
sp_rms_destroy(&rms_r);
63+
if (scale) {
64+
delete[] scale;
65+
scale = nullptr;
66+
}
6367
}
6468

6569
void reset() override {
@@ -68,7 +72,7 @@ void reset() override {
6872
sp_rms_init(sp, rms_l);
6973
sp_rms_init(sp, rms_r);
7074
}
71-
75+
7276
void process(FrameRange range) override {
7377
for (int i : range) {
7478
float speed = speedRamp.getAndStep();
@@ -82,6 +86,9 @@ void process(FrameRange range) override {
8286

8387
float leftOut = 0, rightOut = 0;
8488

89+
pitchcorrect_set_scale_freqs(pitchcorrect_l, scale, scaleCount);
90+
pitchcorrect_set_scale_freqs(pitchcorrect_r, scale, scaleCount);
91+
8592
pitchcorrect_set_speed(pitchcorrect_l, speed);
8693
pitchcorrect_set_amount(pitchcorrect_l, amount);
8794

@@ -103,3 +110,4 @@ void process(FrameRange range) override {
103110
AK_REGISTER_DSP(PitchCorrectDSP, "pcrt")
104111
AK_REGISTER_PARAMETER(PitchCorrectParameterSpeed)
105112
AK_REGISTER_PARAMETER(PitchCorrectParameterAmount)
113+

Sources/SoundpipeAudioKit/Effects/PitchCorrect.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@ public class PitchCorrect: Node {
6464
self.speed = speed
6565
self.amount = amount
6666
}
67+
68+
/// Set the scale frequencies for pitch correction
69+
/// - Parameter frequencies: Array of frequencies in Hz
70+
public func setScaleFrequencies(_ frequencies: [Float]) {
71+
au.setWavetable(frequencies)
72+
}
6773
}

0 commit comments

Comments
 (0)