-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (84 loc) · 3.72 KB
/
main.cpp
File metadata and controls
110 lines (84 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2024 JDSherbert. All rights reserved.
#include <cmath>
#include <iostream>
#include <vector>
#include "Haas.h"
// ======================================================================= //
// Generates a single sample of a sine wave at the given frequency and time.
// frequency - Hz (e.g. 440.0 = concert A)
// time - position in seconds (e.g. sampleIndex / sampleRate)
float GenerateInputSignal(float frequency, float time)
{
return std::sin(2.0f * static_cast<float>(M_PI) * frequency * time);
}
// ======================================================================= //
int main()
{
// == Signal Parameters ==============================================
const int sampleRate = 44100; // 44.1 kHz
const float frequency = 440.0f; // Concert A
const float duration = 0.1f; // 0.1 seconds
const int numSamples = static_cast<int>(sampleRate * duration);
// == Generate Mono Sine Wave ========================================
// The Haas effect is typically applied to a mono source to create
// perceived stereo width. Both channels receive the same input signal.
std::vector<float> monoSignal(numSamples);
for (int i = 0; i < numSamples; ++i)
monoSignal[i] = GenerateInputSignal(frequency, i / static_cast<float>(sampleRate));
// == Demonstrate: Right Channel Delayed (Left Leads) ================
// 20ms delay — well within the 1-40ms Haas range.
// The perceived image will appear to come from the left.
Sherbert::Haas haas(20, Sherbert::Haas::DelayedChannel::Right, static_cast<float>(sampleRate));
const int printCount = 10;
std::cout << "--- Right Channel Delayed, 20ms (sound perceived left) ---\n\n";
for (int i = 0; i < printCount; ++i)
{
const float in = monoSignal[i];
float leftOut = 0.0f;
float rightOut = 0.0f;
haas.ProcessSample(in, in, leftOut, rightOut);
std::cout << "Sample " << i
<< " | In: " << in
<< " | L: " << leftOut
<< " | R: " << rightOut
<< "\n";
}
// == Demonstrate: Left Channel Delayed (Right Leads) ==============
// Reset and flip the delayed channel — perceived image moves right.
haas.reset();
haas.setDelayedChannel(Sherbert::Haas::DelayedChannel::Left);
std::cout << "\n--- Left Channel Delayed, 20ms (sound perceived right) ---\n\n";
for (int i = 0; i < printCount; ++i)
{
const float in = monoSignal[i];
float leftOut = 0.0f;
float rightOut = 0.0f;
haas.ProcessSample(in, in, leftOut, rightOut);
std::cout << "Sample " << i
<< " | In: " << in
<< " | L: " << leftOut
<< " | R: " << rightOut
<< "\n";
}
// == Demonstrate: Delay Time Comparison ==========================
// Show how different delay times affect the output buffer offset.
// At 5ms the channels diverge later than at 20ms.
haas.reset();
haas.setDelayedChannel(Sherbert::Haas::DelayedChannel::Right);
haas.setDelayInMilliseconds(5);
std::cout << "\n--- Right Channel Delayed, 5ms (tighter image) ---\n\n";
for (int i = 0; i < printCount; ++i)
{
const float in = monoSignal[i];
float leftOut = 0.0f;
float rightOut = 0.0f;
haas.ProcessSample(in, in, leftOut, rightOut);
std::cout << "Sample " << i
<< " | In: " << in
<< " | L: " << leftOut
<< " | R: " << rightOut
<< "\n";
}
return 0;
}
// ======================================================================= //