├── .gitignore ├── Bin ├── Mac │ ├── DrumFixer.app │ │ └── Contents │ │ │ ├── Info.plist │ │ │ ├── MacOS │ │ │ └── DrumFixer │ │ │ ├── PkgInfo │ │ │ └── Resources │ │ │ └── RecentFilesMenuTemplate.nib │ ├── DrumFixer.component │ │ └── Contents │ │ │ ├── Info.plist │ │ │ ├── MacOS │ │ │ └── DrumFixer │ │ │ ├── PkgInfo │ │ │ └── Resources │ │ │ ├── DrumFixer.rsrc │ │ │ └── RecentFilesMenuTemplate.nib │ ├── DrumFixer.vst │ │ └── Contents │ │ │ ├── Info.plist │ │ │ ├── MacOS │ │ │ └── DrumFixer │ │ │ ├── PkgInfo │ │ │ └── Resources │ │ │ └── RecentFilesMenuTemplate.nib │ ├── DrumFixer.vst3 │ │ └── Contents │ │ │ ├── Info.plist │ │ │ ├── MacOS │ │ │ └── DrumFixer │ │ │ ├── PkgInfo │ │ │ └── Resources │ │ │ └── RecentFilesMenuTemplate.nib │ └── touch.txt ├── MacBuilds.zip ├── Win │ ├── DrumFixer.dll │ ├── DrumFixer.exe │ └── DrumFixer.vst3 └── WinBuilds.zip ├── DrumFixer ├── DrumFixer.jucer └── Source │ ├── DSP │ ├── DecayFilter.cpp │ ├── DecayFilter.h │ ├── FFTUtils.cpp │ ├── FFTUtils.h │ ├── Filters │ │ ├── Bell.h │ │ ├── Biquad.cpp │ │ ├── Biquad.h │ │ ├── HPF.h │ │ ├── LPF.h │ │ ├── ModeBandpass.cpp │ │ └── ModeBandpass.h │ └── LevelDetectors │ │ ├── BaseDetector.cpp │ │ ├── BaseDetector.h │ │ ├── PeakDetector.h │ │ ├── RMSDetector.h │ │ ├── TransientDetector.cpp │ │ └── TransientDetector.h │ ├── GUI │ ├── FilterList.cpp │ ├── FilterList.h │ ├── SpectrogramComponent.cpp │ ├── SpectrogramComponent.h │ ├── SpectrogramOverlay.cpp │ └── SpectrogramOverlay.h │ ├── PluginEditor.cpp │ ├── PluginEditor.h │ ├── PluginProcessor.cpp │ └── PluginProcessor.h ├── LICENSE ├── README.md ├── Screenshots ├── Edit.gif ├── Filters.gif ├── Listen.gif └── PlotBell.gif ├── Simulations ├── DrumFixerNB.py └── snare_sim.py └── Snares ├── _readme_and_license.txt ├── hi-snare-1.wav ├── low-snare-1.wav ├── low-snare-2.wav ├── rim-shot-hi-no-snap-1.wav ├── rim-shot-hi-snap-1.wav ├── rim-shot-low-no-snap-1.wav └── rim-shot-low-snap-1.wav /.gitignore: -------------------------------------------------------------------------------- 1 | # JUCE content 2 | Builds/ 3 | JuceLibraryCode/ 4 | 5 | # Windows Temp Cache Files 6 | [Tt]humbs.db 7 | 8 | # MacOS Finder stuff 9 | .DS_Dtore 10 | *.DS_Store 11 | *.swp 12 | *~ 13 | 14 | # Visual Studio Code Files 15 | .vscode 16 | 17 | # Demo stuff 18 | *.mp4 19 | *.asd 20 | -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.app/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildMachineOSBuild 6 | 17G65 7 | CFBundleDisplayName 8 | DrumFixer 9 | CFBundleExecutable 10 | DrumFixer 11 | CFBundleIdentifier 12 | com.GASP.DrumFixer 13 | CFBundleName 14 | DrumFixer 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleSupportedPlatforms 22 | 23 | MacOSX 24 | 25 | CFBundleVersion 26 | 1.0.0 27 | DTCompiler 28 | com.apple.compilers.llvm.clang.1_0 29 | DTPlatformBuild 30 | 9F2000 31 | DTPlatformVersion 32 | GM 33 | DTSDKBuild 34 | 17E189 35 | DTSDKName 36 | macosx10.13 37 | DTXcode 38 | 0941 39 | DTXcodeBuild 40 | 9F2000 41 | NSHighResolutionCapable 42 | 43 | NSHumanReadableCopyright 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.app/Contents/MacOS/DrumFixer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.app/Contents/MacOS/DrumFixer -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.app/Contents/PkgInfo: -------------------------------------------------------------------------------- 1 | APPL???? -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.app/Contents/Resources/RecentFilesMenuTemplate.nib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.app/Contents/Resources/RecentFilesMenuTemplate.nib -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.component/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AudioComponents 6 | 7 | 8 | description 9 | DrumFixer 10 | factoryFunction 11 | DrumFixerAUFactory 12 | manufacturer 13 | Manu 14 | name 15 | GASP: DrumFixer 16 | resourceUsage 17 | 18 | network.client 19 | 20 | temporary-exception.files.all.read-write 21 | 22 | 23 | subtype 24 | Wyan 25 | type 26 | aufx 27 | version 28 | 65536 29 | 30 | 31 | BuildMachineOSBuild 32 | 17G65 33 | CFBundleDisplayName 34 | DrumFixer 35 | CFBundleExecutable 36 | DrumFixer 37 | CFBundleIdentifier 38 | com.GASP.DrumFixer 39 | CFBundleName 40 | DrumFixer 41 | CFBundlePackageType 42 | BNDL 43 | CFBundleShortVersionString 44 | 1.0.0 45 | CFBundleSignature 46 | ???? 47 | CFBundleSupportedPlatforms 48 | 49 | MacOSX 50 | 51 | CFBundleVersion 52 | 1.0.0 53 | DTCompiler 54 | com.apple.compilers.llvm.clang.1_0 55 | DTPlatformBuild 56 | 9F2000 57 | DTPlatformVersion 58 | GM 59 | DTSDKBuild 60 | 17E189 61 | DTSDKName 62 | macosx10.13 63 | DTXcode 64 | 0941 65 | DTXcodeBuild 66 | 9F2000 67 | NSHighResolutionCapable 68 | 69 | NSHumanReadableCopyright 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.component/Contents/MacOS/DrumFixer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.component/Contents/MacOS/DrumFixer -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.component/Contents/PkgInfo: -------------------------------------------------------------------------------- 1 | BNDL???? -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.component/Contents/Resources/DrumFixer.rsrc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.component/Contents/Resources/DrumFixer.rsrc -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.component/Contents/Resources/RecentFilesMenuTemplate.nib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.component/Contents/Resources/RecentFilesMenuTemplate.nib -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildMachineOSBuild 6 | 17G65 7 | CFBundleDisplayName 8 | DrumFixer 9 | CFBundleExecutable 10 | DrumFixer 11 | CFBundleIdentifier 12 | com.GASP.DrumFixer 13 | CFBundleName 14 | DrumFixer 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleSupportedPlatforms 22 | 23 | MacOSX 24 | 25 | CFBundleVersion 26 | 1.0.0 27 | DTCompiler 28 | com.apple.compilers.llvm.clang.1_0 29 | DTPlatformBuild 30 | 9F2000 31 | DTPlatformVersion 32 | GM 33 | DTSDKBuild 34 | 17E189 35 | DTSDKName 36 | macosx10.13 37 | DTXcode 38 | 0941 39 | DTXcodeBuild 40 | 9F2000 41 | NSHighResolutionCapable 42 | 43 | NSHumanReadableCopyright 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst/Contents/MacOS/DrumFixer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.vst/Contents/MacOS/DrumFixer -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst/Contents/PkgInfo: -------------------------------------------------------------------------------- 1 | BNDL???? -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst/Contents/Resources/RecentFilesMenuTemplate.nib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.vst/Contents/Resources/RecentFilesMenuTemplate.nib -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst3/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildMachineOSBuild 6 | 17G65 7 | CFBundleDisplayName 8 | DrumFixer 9 | CFBundleExecutable 10 | DrumFixer 11 | CFBundleIdentifier 12 | com.GASP.DrumFixer 13 | CFBundleName 14 | DrumFixer 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleSupportedPlatforms 22 | 23 | MacOSX 24 | 25 | CFBundleVersion 26 | 1.0.0 27 | DTCompiler 28 | com.apple.compilers.llvm.clang.1_0 29 | DTPlatformBuild 30 | 9F2000 31 | DTPlatformVersion 32 | GM 33 | DTSDKBuild 34 | 17E189 35 | DTSDKName 36 | macosx10.13 37 | DTXcode 38 | 0941 39 | DTXcodeBuild 40 | 9F2000 41 | NSHighResolutionCapable 42 | 43 | NSHumanReadableCopyright 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst3/Contents/MacOS/DrumFixer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.vst3/Contents/MacOS/DrumFixer -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst3/Contents/PkgInfo: -------------------------------------------------------------------------------- 1 | BNDL???? -------------------------------------------------------------------------------- /Bin/Mac/DrumFixer.vst3/Contents/Resources/RecentFilesMenuTemplate.nib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/DrumFixer.vst3/Contents/Resources/RecentFilesMenuTemplate.nib -------------------------------------------------------------------------------- /Bin/Mac/touch.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Mac/touch.txt -------------------------------------------------------------------------------- /Bin/MacBuilds.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/MacBuilds.zip -------------------------------------------------------------------------------- /Bin/Win/DrumFixer.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Win/DrumFixer.dll -------------------------------------------------------------------------------- /Bin/Win/DrumFixer.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Win/DrumFixer.exe -------------------------------------------------------------------------------- /Bin/Win/DrumFixer.vst3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/Win/DrumFixer.vst3 -------------------------------------------------------------------------------- /Bin/WinBuilds.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Bin/WinBuilds.zip -------------------------------------------------------------------------------- /DrumFixer/DrumFixer.jucer: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 42 | 44 | 46 | 47 | 49 | 51 | 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 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/DecayFilter.cpp: -------------------------------------------------------------------------------- 1 | #include "DecayFilter.h" 2 | #include "Filters/ModeBandpass.h" 3 | #include "LevelDetectors/RMSDetector.h" 4 | #include 5 | 6 | DecayFilter::DecayFilter (Params& params, const AudioBuffer& audio, double fs) : 7 | params (params), 8 | sampleBuffer (audio), 9 | sampleFs (fs) 10 | { 11 | updateFilter(); 12 | } 13 | 14 | std::unique_ptr DecayFilter::toXml() 15 | { 16 | auto filterXml = std::make_unique ("FilterState"); 17 | 18 | filterXml->setAttribute ("CenterFreq", params.centerFreq); 19 | filterXml->setAttribute ("Bandwidth", params.bandwidth); 20 | filterXml->setAttribute ("DesiredT60", params.desiredT60); 21 | 22 | return filterXml; 23 | } 24 | 25 | std::unique_ptr DecayFilter::fromXml (XmlElement* xml, const AudioBuffer& buffer, float fs) 26 | { 27 | auto fc = (float) xml->getDoubleAttribute ("CenterFreq"); 28 | auto bw = (float) xml->getDoubleAttribute ("Bandwidth"); 29 | auto dt60 = (float) xml->getDoubleAttribute ("DesiredT60"); 30 | 31 | Params params (fc, bw, dt60); 32 | return std::make_unique (params, buffer, fs); 33 | } 34 | 35 | void DecayFilter::updateFilter() 36 | { 37 | actualT60 = getActualT60 (params, sampleBuffer, sampleFs); 38 | auto filterQ = params.centerFreq / jmax (params.bandwidth, 0.1f); 39 | 40 | auto gainDes = getGainForT60 (params.desiredT60, sampleRate); 41 | auto gainOrig = getGainForT60 (actualT60, sampleRate); 42 | filtGain = gainDes / gainOrig; 43 | 44 | for (int ch = 0; ch < 2; ++ch) 45 | bell[ch].calcCoefs (params.centerFreq, filterQ, -60.0f); 46 | } 47 | 48 | void DecayFilter::prepare (double fs, int samplesPerBlock) 49 | { 50 | sampleRate = (float) fs; 51 | updateFilter(); 52 | 53 | filtBuffer.setSize (2, samplesPerBlock); 54 | } 55 | 56 | void DecayFilter::processBlock (AudioBuffer& buffer) 57 | { 58 | if (curGain[0] == 0.0f && sampleIdx < 0) // nothing to do... 59 | return; 60 | 61 | // do processing 62 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 63 | { 64 | filtBuffer.copyFrom (ch, 0, buffer, ch, 0, buffer.getNumSamples()); 65 | bell[ch].processBlock (filtBuffer.getWritePointer (ch), buffer.getNumSamples()); 66 | } 67 | 68 | if (sampleIdx > 0) // new transient 69 | { 70 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 71 | { 72 | auto* filtData = filtBuffer.getReadPointer (ch); 73 | auto* xData = buffer.getWritePointer (ch); 74 | 75 | curGain[ch] = 1.0f; 76 | for (int n = sampleIdx; n < buffer.getNumSamples(); ++n) 77 | { 78 | xData[n] = filtData[n] * (1.0f - curGain[ch]) + xData[n] * curGain[ch]; 79 | curGain[ch] *= filtGain; 80 | } 81 | } 82 | 83 | sampleIdx = -1; 84 | } 85 | else 86 | { 87 | // check if filter has been running for too long (and reset if it has) 88 | if (curGain[0] < Decibels::decibelsToGain (-60.0f) 89 | || curGain[0] > Decibels::decibelsToGain (30.0f)) 90 | { 91 | curGain[0] = 0.0f; curGain[1] = 0.0f; 92 | return; 93 | } 94 | 95 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 96 | { 97 | auto* filtData = filtBuffer.getReadPointer (ch); 98 | auto* xData = buffer.getWritePointer (ch); 99 | 100 | for (int n = 0; n < buffer.getNumSamples(); ++n) 101 | { 102 | xData[n] = filtData[n] * (1.0f - curGain[ch]) + xData[n] * curGain[ch]; 103 | curGain[ch] *= filtGain; 104 | } 105 | } 106 | } 107 | } 108 | 109 | float DecayFilter::getActualT60 (Params& p, const AudioBuffer& audio, double fs) 110 | { 111 | if (audio.hasBeenCleared()) 112 | return 0.1f; 113 | 114 | // set up analysis buffer 115 | auto nChannels = audio.getNumChannels(); 116 | AudioBuffer analysisBuffer; 117 | analysisBuffer.setSize (1, audio.getNumSamples()); 118 | for (int ch = 0; ch < nChannels; ++ch) 119 | analysisBuffer.addFrom (0, 0, audio, ch, 0, audio.getNumSamples(), 1.0f / (float) nChannels); 120 | 121 | // Filter around mode 122 | ModeBandpass modeBPF (p.centerFreq, p.bandwidth, fs); 123 | modeBPF.processBlock (analysisBuffer); 124 | 125 | // Do RMS level detection 126 | RMSDetector rmsDetector; 127 | rmsDetector.reset ((float) fs); 128 | rmsDetector.setAttackMs (10.0f); 129 | rmsDetector.setReleaseMs (50.0f); 130 | rmsDetector.processBlock (analysisBuffer.getWritePointer (0), analysisBuffer.getNumSamples()); 131 | 132 | auto magnitude = analysisBuffer.getMagnitude (0, audio.getNumSamples()); 133 | analysisBuffer.applyGain (1.0f / magnitude); 134 | 135 | // compute slope 136 | std::vector timeSamples; 137 | std::vector envelopeDB; 138 | int sampCount = 0; 139 | const float dBTime = -30.0f; 140 | for (int n = 0; n < audio.getNumSamples(); ++n) 141 | { 142 | auto sampleDB = Decibels::gainToDecibels (analysisBuffer.getSample (0, n)); 143 | if (sampleDB < -1.0f && sampCount == 0) 144 | continue; 145 | 146 | if (sampleDB < dBTime && sampCount > 0) 147 | break; 148 | 149 | timeSamples.push_back ((float) sampCount++); 150 | envelopeDB.push_back (sampleDB); 151 | } 152 | 153 | float slope = 0.0f; 154 | if (timeSamples.size() > 0) 155 | slope = getSlope (timeSamples, envelopeDB); 156 | 157 | return (dBTime / slope) / (float) fs; 158 | } 159 | 160 | float DecayFilter::getSlope (const std::vector x, const std::vector y) 161 | { 162 | const auto n = x.size(); 163 | const auto s_x = std::accumulate (x.begin(), x.end(), 0.0f); 164 | const auto s_y = std::accumulate (y.begin(), y.end(), 0.0f); 165 | const auto s_xx = std::inner_product (x.begin(), x.end(), x.begin(), 0.0f); 166 | const auto s_xy = std::inner_product (x.begin(), x.end(), y.begin(), 0.0f); 167 | const auto a = (n * s_xy - s_x * s_y) / (n * s_xx - s_x * s_x); 168 | return a; 169 | } 170 | 171 | float DecayFilter::getGainForT60 (float t60, float sampleRate) 172 | { 173 | return pow (0.001f, 1.0f / (t60 * sampleRate)); 174 | } 175 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/DecayFilter.h: -------------------------------------------------------------------------------- 1 | #ifndef DECAYFILTER_H_INCLUDED 2 | #define DECAYFILTER_H_INCLUDED 3 | 4 | #include "Filters/Bell.h" 5 | 6 | class DecayFilter 7 | { 8 | public: 9 | struct Params 10 | { 11 | Params (float fc, float bw, float tau) : 12 | centerFreq (fc), 13 | bandwidth (bw), 14 | desiredT60 (tau) 15 | {} 16 | 17 | Params (Params& p) : 18 | centerFreq (p.centerFreq), 19 | bandwidth (p.bandwidth), 20 | desiredT60 (p.desiredT60) 21 | {} 22 | 23 | float centerFreq; 24 | float bandwidth; 25 | float desiredT60; 26 | }; 27 | 28 | DecayFilter (Params& params, const AudioBuffer& audio, double fs); 29 | 30 | Params& getParams() { return params; } 31 | 32 | void updateFilter(); 33 | void prepare (double sampleRate, int samplesPerBlock); 34 | void processBlock (AudioBuffer& buffer); 35 | void newTransient (int newSampleIdx) { sampleIdx = newSampleIdx; } 36 | 37 | std::unique_ptr toXml(); 38 | static std::unique_ptr fromXml (XmlElement* xml, const AudioBuffer& buffer, float fs); 39 | 40 | bool isSelected() const noexcept { return selected; } 41 | void setSelected (bool shouldBeSelected) { selected = shouldBeSelected; } 42 | 43 | private: 44 | bool selected = false; 45 | 46 | static float getActualT60 (Params& params, const AudioBuffer& audio, double fs); 47 | static float getSlope (const std::vector x, const std::vector y); 48 | static float getGainForT60 (float t60, float sampleRate); 49 | 50 | Params params; 51 | float actualT60; 52 | const float sampleFs = 48000.0f; 53 | const AudioBuffer& sampleBuffer; 54 | 55 | float sampleRate = 48000.0f; 56 | BellFilter bell[2]; 57 | AudioBuffer filtBuffer; 58 | 59 | int sampleIdx = -1; 60 | float curGain[2] = { 0.0f, 0.0f }; 61 | float filtGain = 1.0f; 62 | 63 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DecayFilter) 64 | }; 65 | 66 | #endif // DECAYFILTER_H_INCLUDED 67 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/FFTUtils.cpp: -------------------------------------------------------------------------------- 1 | #include "FFTUtils.h" 2 | 3 | namespace 4 | { 5 | constexpr float lowFreq = 50.0f; 6 | constexpr float highFreq = 20000.0f; 7 | }; 8 | 9 | FFTUtils::FFTUtils() : 10 | forwardFFT (fftOrder) 11 | { 12 | } 13 | 14 | void FFTUtils::processBlock (AudioBuffer& buffer) 15 | { 16 | auto data = buffer.getArrayOfReadPointers(); 17 | for (int n = 0; n < buffer.getNumSamples(); ++n) 18 | { 19 | float sample = 0.0f; 20 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 21 | sample += abs (data[ch][n]); 22 | 23 | pushNextSampleIntoFifo (sample / (float) buffer.getNumChannels()); 24 | } 25 | } 26 | 27 | void FFTUtils::pushNextSampleIntoFifo (float sample) noexcept 28 | { 29 | if (fifoIndex == fftSize) 30 | { 31 | if (! nextFFTBlockReady) 32 | { 33 | zeromem (fftData, sizeof (fftData)); 34 | memcpy (fftData, fifo, sizeof (fifo)); 35 | nextFFTBlockReady = true; 36 | } 37 | 38 | fifoIndex = 0; 39 | } 40 | 41 | fifo[fifoIndex++] = sample; 42 | } 43 | 44 | void FFTUtils::drawNextLineOfSpectrogram (Array& data, int height) 45 | { 46 | const auto imageHeight = (float) height; 47 | 48 | // render FFT data... 49 | forwardFFT.performFrequencyOnlyForwardTransform (fftData); 50 | const float minDB = -60.0f; 51 | for (int k = 0; k < fftSize/2; ++k) 52 | fftDataDB[k] = Decibels::gainToDecibels (fftData[k] / 200, minDB); 53 | 54 | const float binWidth = sampleRate / (float) fftSize; 55 | 56 | for (auto y = 0; y < imageHeight; ++y) 57 | { 58 | const auto freq = yToFreq ((float) y, (float) imageHeight); 59 | const auto fftIdx = jmin (freq / binWidth, (float) fftSize / 2); 60 | auto lower = fftDataDB[int (floor (fftIdx))]; 61 | auto upper = fftDataDB[int (ceil (fftIdx))]; 62 | const auto frac = fftIdx - floor (fftIdx); 63 | auto interp = upper * frac + lower * (1.0f - frac); 64 | 65 | auto level = jmap (interp, minDB, 0.0f, 0.0f, 1.0f); 66 | data.add (getInfernoColour (level)); 67 | } 68 | 69 | nextFFTBlockReady = false; 70 | } 71 | 72 | Colour FFTUtils::getInfernoColour (float level) 73 | { 74 | auto colourVals = inferno[int (level * 256)]; 75 | return Colour::fromFloatRGBA (colourVals[0], colourVals[1], colourVals[2], 1.0f); 76 | } 77 | 78 | float FFTUtils::yToFreq (float y, float height) 79 | { 80 | return lowFreq * pow ((highFreq / lowFreq), 1.0f - (y / height)); 81 | } 82 | 83 | int FFTUtils::freqToY (float freq, float height) 84 | { 85 | return int ((1.0f - (log (freq / lowFreq) / log (highFreq / lowFreq))) * height); 86 | } 87 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/FFTUtils.h: -------------------------------------------------------------------------------- 1 | #ifndef FFTUTILS_H_INCLUDED 2 | #define FFTUTILS_H_INCLUDED 3 | 4 | #include "JuceHeader.h" 5 | 6 | class FFTUtils 7 | { 8 | public: 9 | FFTUtils(); 10 | 11 | void processBlock (AudioBuffer& buffer); 12 | void pushNextSampleIntoFifo (float sample) noexcept; 13 | void setSampleRate (double sr) { sampleRate = (float) sr; } 14 | 15 | static float yToFreq (float y, float height); 16 | static int freqToY (float freq, float height); 17 | Colour getInfernoColour (float level); 18 | 19 | bool isFFTReady() { return nextFFTBlockReady; } 20 | void drawNextLineOfSpectrogram (Array& data, int height); 21 | 22 | private: 23 | enum 24 | { 25 | fftOrder = 10, 26 | fftSize = 1 << fftOrder 27 | }; 28 | 29 | dsp::FFT forwardFFT; 30 | 31 | float fifo [fftSize]; 32 | float fftData [2 * fftSize]; 33 | float fftDataDB [2 * fftSize]; 34 | int fifoIndex = 0; 35 | bool nextFFTBlockReady = false; 36 | 37 | float sampleRate = 48000.0f; 38 | 39 | float inferno[256][3] = {{0.001462f, 0.000466f, 0.013866f}, 40 | {0.002267f, 0.001270f, 0.018570f}, 41 | {0.003299f, 0.002249f, 0.024239f}, 42 | {0.004547f, 0.003392f, 0.030909f}, 43 | {0.006006f, 0.004692f, 0.038558f}, 44 | {0.007676f, 0.006136f, 0.046836f}, 45 | {0.009561f, 0.007713f, 0.055143f}, 46 | {0.011663f, 0.009417f, 0.063460f}, 47 | {0.013995f, 0.011225f, 0.071862f}, 48 | {0.016561f, 0.013136f, 0.080282f}, 49 | {0.019373f, 0.015133f, 0.088767f}, 50 | {0.022447f, 0.017199f, 0.097327f}, 51 | {0.025793f, 0.019331f, 0.105930f}, 52 | {0.029432f, 0.021503f, 0.114621f}, 53 | {0.033385f, 0.023702f, 0.123397f}, 54 | {0.037668f, 0.025921f, 0.132232f}, 55 | {0.042253f, 0.028139f, 0.141141f}, 56 | {0.046915f, 0.030324f, 0.150164f}, 57 | {0.051644f, 0.032474f, 0.159254f}, 58 | {0.056449f, 0.034569f, 0.168414f}, 59 | {0.061340f, 0.036590f, 0.177642f}, 60 | {0.066331f, 0.038504f, 0.186962f}, 61 | {0.071429f, 0.040294f, 0.196354f}, 62 | {0.076637f, 0.041905f, 0.205799f}, 63 | {0.081962f, 0.043328f, 0.215289f}, 64 | {0.087411f, 0.044556f, 0.224813f}, 65 | {0.092990f, 0.045583f, 0.234358f}, 66 | {0.098702f, 0.046402f, 0.243904f}, 67 | {0.104551f, 0.047008f, 0.253430f}, 68 | {0.110536f, 0.047399f, 0.262912f}, 69 | {0.116656f, 0.047574f, 0.272321f}, 70 | {0.122908f, 0.047536f, 0.281624f}, 71 | {0.129285f, 0.047293f, 0.290788f}, 72 | {0.135778f, 0.046856f, 0.299776f}, 73 | {0.142378f, 0.046242f, 0.308553f}, 74 | {0.149073f, 0.045468f, 0.317085f}, 75 | {0.155850f, 0.044559f, 0.325338f}, 76 | {0.162689f, 0.043554f, 0.333277f}, 77 | {0.169575f, 0.042489f, 0.340874f}, 78 | {0.176493f, 0.041402f, 0.348111f}, 79 | {0.183429f, 0.040329f, 0.354971f}, 80 | {0.190367f, 0.039309f, 0.361447f}, 81 | {0.197297f, 0.038400f, 0.367535f}, 82 | {0.204209f, 0.037632f, 0.373238f}, 83 | {0.211095f, 0.037030f, 0.378563f}, 84 | {0.217949f, 0.036615f, 0.383522f}, 85 | {0.224763f, 0.036405f, 0.388129f}, 86 | {0.231538f, 0.036405f, 0.392400f}, 87 | {0.238273f, 0.036621f, 0.396353f}, 88 | {0.244967f, 0.037055f, 0.400007f}, 89 | {0.251620f, 0.037705f, 0.403378f}, 90 | {0.258234f, 0.038571f, 0.406485f}, 91 | {0.264810f, 0.039647f, 0.409345f}, 92 | {0.271347f, 0.040922f, 0.411976f}, 93 | {0.277850f, 0.042353f, 0.414392f}, 94 | {0.284321f, 0.043933f, 0.416608f}, 95 | {0.290763f, 0.045644f, 0.418637f}, 96 | {0.297178f, 0.047470f, 0.420491f}, 97 | {0.303568f, 0.049396f, 0.422182f}, 98 | {0.309935f, 0.051407f, 0.423721f}, 99 | {0.316282f, 0.053490f, 0.425116f}, 100 | {0.322610f, 0.055634f, 0.426377f}, 101 | {0.328921f, 0.057827f, 0.427511f}, 102 | {0.335217f, 0.060060f, 0.428524f}, 103 | {0.341500f, 0.062325f, 0.429425f}, 104 | {0.347771f, 0.064616f, 0.430217f}, 105 | {0.354032f, 0.066925f, 0.430906f}, 106 | {0.360284f, 0.069247f, 0.431497f}, 107 | {0.366529f, 0.071579f, 0.431994f}, 108 | {0.372768f, 0.073915f, 0.432400f}, 109 | {0.379001f, 0.076253f, 0.432719f}, 110 | {0.385228f, 0.078591f, 0.432955f}, 111 | {0.391453f, 0.080927f, 0.433109f}, 112 | {0.397674f, 0.083257f, 0.433183f}, 113 | {0.403894f, 0.085580f, 0.433179f}, 114 | {0.410113f, 0.087896f, 0.433098f}, 115 | {0.416331f, 0.090203f, 0.432943f}, 116 | {0.422549f, 0.092501f, 0.432714f}, 117 | {0.428768f, 0.094790f, 0.432412f}, 118 | {0.434987f, 0.097069f, 0.432039f}, 119 | {0.441207f, 0.099338f, 0.431594f}, 120 | {0.447428f, 0.101597f, 0.431080f}, 121 | {0.453651f, 0.103848f, 0.430498f}, 122 | {0.459875f, 0.106089f, 0.429846f}, 123 | {0.466100f, 0.108322f, 0.429125f}, 124 | {0.472328f, 0.110547f, 0.428334f}, 125 | {0.478558f, 0.112764f, 0.427475f}, 126 | {0.484789f, 0.114974f, 0.426548f}, 127 | {0.491022f, 0.117179f, 0.425552f}, 128 | {0.497257f, 0.119379f, 0.424488f}, 129 | {0.503493f, 0.121575f, 0.423356f}, 130 | {0.509730f, 0.123769f, 0.422156f}, 131 | {0.515967f, 0.125960f, 0.420887f}, 132 | {0.522206f, 0.128150f, 0.419549f}, 133 | {0.528444f, 0.130341f, 0.418142f}, 134 | {0.534683f, 0.132534f, 0.416667f}, 135 | {0.540920f, 0.134729f, 0.415123f}, 136 | {0.547157f, 0.136929f, 0.413511f}, 137 | {0.553392f, 0.139134f, 0.411829f}, 138 | {0.559624f, 0.141346f, 0.410078f}, 139 | {0.565854f, 0.143567f, 0.408258f}, 140 | {0.572081f, 0.145797f, 0.406369f}, 141 | {0.578304f, 0.148039f, 0.404411f}, 142 | {0.584521f, 0.150294f, 0.402385f}, 143 | {0.590734f, 0.152563f, 0.400290f}, 144 | {0.596940f, 0.154848f, 0.398125f}, 145 | {0.603139f, 0.157151f, 0.395891f}, 146 | {0.609330f, 0.159474f, 0.393589f}, 147 | {0.615513f, 0.161817f, 0.391219f}, 148 | {0.621685f, 0.164184f, 0.388781f}, 149 | {0.627847f, 0.166575f, 0.386276f}, 150 | {0.633998f, 0.168992f, 0.383704f}, 151 | {0.640135f, 0.171438f, 0.381065f}, 152 | {0.646260f, 0.173914f, 0.378359f}, 153 | {0.652369f, 0.176421f, 0.375586f}, 154 | {0.658463f, 0.178962f, 0.372748f}, 155 | {0.664540f, 0.181539f, 0.369846f}, 156 | {0.670599f, 0.184153f, 0.366879f}, 157 | {0.676638f, 0.186807f, 0.363849f}, 158 | {0.682656f, 0.189501f, 0.360757f}, 159 | {0.688653f, 0.192239f, 0.357603f}, 160 | {0.694627f, 0.195021f, 0.354388f}, 161 | {0.700576f, 0.197851f, 0.351113f}, 162 | {0.706500f, 0.200728f, 0.347777f}, 163 | {0.712396f, 0.203656f, 0.344383f}, 164 | {0.718264f, 0.206636f, 0.340931f}, 165 | {0.724103f, 0.209670f, 0.337424f}, 166 | {0.729909f, 0.212759f, 0.333861f}, 167 | {0.735683f, 0.215906f, 0.330245f}, 168 | {0.741423f, 0.219112f, 0.326576f}, 169 | {0.747127f, 0.222378f, 0.322856f}, 170 | {0.752794f, 0.225706f, 0.319085f}, 171 | {0.758422f, 0.229097f, 0.315266f}, 172 | {0.764010f, 0.232554f, 0.311399f}, 173 | {0.769556f, 0.236077f, 0.307485f}, 174 | {0.775059f, 0.239667f, 0.303526f}, 175 | {0.780517f, 0.243327f, 0.299523f}, 176 | {0.785929f, 0.247056f, 0.295477f}, 177 | {0.791293f, 0.250856f, 0.291390f}, 178 | {0.796607f, 0.254728f, 0.287264f}, 179 | {0.801871f, 0.258674f, 0.283099f}, 180 | {0.807082f, 0.262692f, 0.278898f}, 181 | {0.812239f, 0.266786f, 0.274661f}, 182 | {0.817341f, 0.270954f, 0.270390f}, 183 | {0.822386f, 0.275197f, 0.266085f}, 184 | {0.827372f, 0.279517f, 0.261750f}, 185 | {0.832299f, 0.283913f, 0.257383f}, 186 | {0.837165f, 0.288385f, 0.252988f}, 187 | {0.841969f, 0.292933f, 0.248564f}, 188 | {0.846709f, 0.297559f, 0.244113f}, 189 | {0.851384f, 0.302260f, 0.239636f}, 190 | {0.855992f, 0.307038f, 0.235133f}, 191 | {0.860533f, 0.311892f, 0.230606f}, 192 | {0.865006f, 0.316822f, 0.226055f}, 193 | {0.869409f, 0.321827f, 0.221482f}, 194 | {0.873741f, 0.326906f, 0.216886f}, 195 | {0.878001f, 0.332060f, 0.212268f}, 196 | {0.882188f, 0.337287f, 0.207628f}, 197 | {0.886302f, 0.342586f, 0.202968f}, 198 | {0.890341f, 0.347957f, 0.198286f}, 199 | {0.894305f, 0.353399f, 0.193584f}, 200 | {0.898192f, 0.358911f, 0.188860f}, 201 | {0.902003f, 0.364492f, 0.184116f}, 202 | {0.905735f, 0.370140f, 0.179350f}, 203 | {0.909390f, 0.375856f, 0.174563f}, 204 | {0.912966f, 0.381636f, 0.169755f}, 205 | {0.916462f, 0.387481f, 0.164924f}, 206 | {0.919879f, 0.393389f, 0.160070f}, 207 | {0.923215f, 0.399359f, 0.155193f}, 208 | {0.926470f, 0.405389f, 0.150292f}, 209 | {0.929644f, 0.411479f, 0.145367f}, 210 | {0.932737f, 0.417627f, 0.140417f}, 211 | {0.935747f, 0.423831f, 0.135440f}, 212 | {0.938675f, 0.430091f, 0.130438f}, 213 | {0.941521f, 0.436405f, 0.125409f}, 214 | {0.944285f, 0.442772f, 0.120354f}, 215 | {0.946965f, 0.449191f, 0.115272f}, 216 | {0.949562f, 0.455660f, 0.110164f}, 217 | {0.952075f, 0.462178f, 0.105031f}, 218 | {0.954506f, 0.468744f, 0.099874f}, 219 | {0.956852f, 0.475356f, 0.094695f}, 220 | {0.959114f, 0.482014f, 0.089499f}, 221 | {0.961293f, 0.488716f, 0.084289f}, 222 | {0.963387f, 0.495462f, 0.079073f}, 223 | {0.965397f, 0.502249f, 0.073859f}, 224 | {0.967322f, 0.509078f, 0.068659f}, 225 | {0.969163f, 0.515946f, 0.063488f}, 226 | {0.970919f, 0.522853f, 0.058367f}, 227 | {0.972590f, 0.529798f, 0.053324f}, 228 | {0.974176f, 0.536780f, 0.048392f}, 229 | {0.975677f, 0.543798f, 0.043618f}, 230 | {0.977092f, 0.550850f, 0.039050f}, 231 | {0.978422f, 0.557937f, 0.034931f}, 232 | {0.979666f, 0.565057f, 0.031409f}, 233 | {0.980824f, 0.572209f, 0.028508f}, 234 | {0.981895f, 0.579392f, 0.026250f}, 235 | {0.982881f, 0.586606f, 0.024661f}, 236 | {0.983779f, 0.593849f, 0.023770f}, 237 | {0.984591f, 0.601122f, 0.023606f}, 238 | {0.985315f, 0.608422f, 0.024202f}, 239 | {0.985952f, 0.615750f, 0.025592f}, 240 | {0.986502f, 0.623105f, 0.027814f}, 241 | {0.986964f, 0.630485f, 0.030908f}, 242 | {0.987337f, 0.637890f, 0.034916f}, 243 | {0.987622f, 0.645320f, 0.039886f}, 244 | {0.987819f, 0.652773f, 0.045581f}, 245 | {0.987926f, 0.660250f, 0.051750f}, 246 | {0.987945f, 0.667748f, 0.058329f}, 247 | {0.987874f, 0.675267f, 0.065257f}, 248 | {0.987714f, 0.682807f, 0.072489f}, 249 | {0.987464f, 0.690366f, 0.079990f}, 250 | {0.987124f, 0.697944f, 0.087731f}, 251 | {0.986694f, 0.705540f, 0.095694f}, 252 | {0.986175f, 0.713153f, 0.103863f}, 253 | {0.985566f, 0.720782f, 0.112229f}, 254 | {0.984865f, 0.728427f, 0.120785f}, 255 | {0.984075f, 0.736087f, 0.129527f}, 256 | {0.983196f, 0.743758f, 0.138453f}, 257 | {0.982228f, 0.751442f, 0.147565f}, 258 | {0.981173f, 0.759135f, 0.156863f}, 259 | {0.980032f, 0.766837f, 0.166353f}, 260 | {0.978806f, 0.774545f, 0.176037f}, 261 | {0.977497f, 0.782258f, 0.185923f}, 262 | {0.976108f, 0.789974f, 0.196018f}, 263 | {0.974638f, 0.797692f, 0.206332f}, 264 | {0.973088f, 0.805409f, 0.216877f}, 265 | {0.971468f, 0.813122f, 0.227658f}, 266 | {0.969783f, 0.820825f, 0.238686f}, 267 | {0.968041f, 0.828515f, 0.249972f}, 268 | {0.966243f, 0.836191f, 0.261534f}, 269 | {0.964394f, 0.843848f, 0.273391f}, 270 | {0.962517f, 0.851476f, 0.285546f}, 271 | {0.960626f, 0.859069f, 0.298010f}, 272 | {0.958720f, 0.866624f, 0.310820f}, 273 | {0.956834f, 0.874129f, 0.323974f}, 274 | {0.954997f, 0.881569f, 0.337475f}, 275 | {0.953215f, 0.888942f, 0.351369f}, 276 | {0.951546f, 0.896226f, 0.365627f}, 277 | {0.950018f, 0.903409f, 0.380271f}, 278 | {0.948683f, 0.910473f, 0.395289f}, 279 | {0.947594f, 0.917399f, 0.410665f}, 280 | {0.946809f, 0.924168f, 0.426373f}, 281 | {0.946392f, 0.930761f, 0.442367f}, 282 | {0.946403f, 0.937159f, 0.458592f}, 283 | {0.946903f, 0.943348f, 0.474970f}, 284 | {0.947937f, 0.949318f, 0.491426f}, 285 | {0.949545f, 0.955063f, 0.507860f}, 286 | {0.951740f, 0.960587f, 0.524203f}, 287 | {0.954529f, 0.965896f, 0.540361f}, 288 | {0.957896f, 0.971003f, 0.556275f}, 289 | {0.961812f, 0.975924f, 0.571925f}, 290 | {0.966249f, 0.980678f, 0.587206f}, 291 | {0.971162f, 0.985282f, 0.602154f}, 292 | {0.976511f, 0.989753f, 0.616760f}, 293 | {0.982257f, 0.994109f, 0.631017f}, 294 | {0.988362f, 0.998364f, 0.644924f}}; 295 | 296 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFTUtils) 297 | }; 298 | 299 | #endif // FFTUTILS_H_INCLUDED 300 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/Bell.h: -------------------------------------------------------------------------------- 1 | #ifndef BELL_H_INCLUDED 2 | #define BELL_H_INCLUDED 3 | 4 | #include "Biquad.h" 5 | 6 | class BellFilter : public Biquad 7 | { 8 | public: 9 | BellFilter() {} 10 | 11 | void calcCoefs (float newFc, float newQ, float newGainDB = 0.0f) override 12 | { 13 | fc = newFc; 14 | Q = newQ; 15 | gainDB = newGainDB; 16 | 17 | float wc = MathConstants::twoPi * fc / fs; 18 | float c = 1.0f / tan (wc / 2.0f); 19 | float phi = c * c; 20 | float Knum = c / Q; 21 | float Kdenom = Knum; 22 | 23 | if (gainDB > 0.0f) 24 | Knum *= Decibels::decibelsToGain (gainDB); 25 | else if (gainDB < 0.0f) 26 | Kdenom /= Decibels::decibelsToGain (gainDB); 27 | 28 | float a0 = phi + Kdenom + 1.0f; 29 | 30 | b[0] = (phi + Knum + 1.0f) / a0; 31 | b[1] = 2.0f * (1.0f - phi) / a0; 32 | b[2] = (phi - Knum + 1.0f) / a0; 33 | 34 | a[1] = 2.0f * (1.0f - phi) / a0; 35 | a[2] = (phi - Kdenom + 1.0f) / a0; 36 | } 37 | 38 | private: 39 | 40 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BellFilter) 41 | }; 42 | 43 | #endif // BELL_H_INCLUDED 44 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/Biquad.cpp: -------------------------------------------------------------------------------- 1 | #include "Biquad.h" 2 | 3 | void Biquad::reset (double sampleRate) 4 | { 5 | // clear filter state 6 | for (int n = 0; n <= order; ++n) 7 | z[n] = 0.0f; 8 | 9 | fs = (float) sampleRate; 10 | 11 | calcCoefs (fc, Q, gainDB); 12 | } 13 | 14 | void Biquad::processBlock (float* buffer, int numSamples) 15 | { 16 | for (int n = 0; n < numSamples; ++n) 17 | buffer[n] = process (buffer[n]); 18 | } 19 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/Biquad.h: -------------------------------------------------------------------------------- 1 | #ifndef BIQUAD_H_INCLUDED 2 | #define BIQUAD_H_INCLUDED 3 | 4 | #include "JuceHeader.h" 5 | 6 | class Biquad 7 | { 8 | public: 9 | Biquad() {} 10 | virtual ~Biquad() {} 11 | 12 | virtual void calcCoefs (float /*fc*/, float /*Q*/, float /*gainDB*/) = 0; 13 | 14 | virtual void reset (double sampleRate); 15 | virtual void processBlock (float* buffer, int numSamples); 16 | 17 | virtual inline float process (float x) 18 | { 19 | // Direct Form II transposed 20 | float y = z[1] + x * b[0]; 21 | z[1] = z[2] + x*b[1] - y*a[1]; 22 | z[2] = x*b[2] - y*a[2]; 23 | 24 | return y; 25 | } 26 | 27 | protected: 28 | float fs = 48000.0f; 29 | 30 | float a[3] = { 1.0f, 0.0f, 0.0f }; 31 | float b[3] = { 1.0f, 0.0f, 0.0f }; 32 | float z[3] = { 1.0f, 0.0f, 0.0f }; 33 | 34 | float fc = 1000.0f; 35 | float Q = 0.7071f; 36 | float gainDB = 0.0f; 37 | 38 | private: 39 | const int order = 2; 40 | 41 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Biquad) 42 | }; 43 | 44 | #endif // BIQUAD_H_INCLUDED 45 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/HPF.h: -------------------------------------------------------------------------------- 1 | #ifndef HPF_H_INCLUDED 2 | #define HPF_H_INCLUDED 3 | 4 | #include "Biquad.h" 5 | 6 | class HPF2 : public Biquad 7 | { 8 | public: 9 | HPF2() {} 10 | 11 | void calcCoefs (float newFc, float newQ, float newGainDB = 0.0f) override 12 | { 13 | fc = newFc; 14 | Q = newQ; 15 | gainDB = newGainDB; 16 | 17 | float wc = MathConstants::twoPi * fc / fs; 18 | float c = 1.0f / tan (wc / 2.0f); 19 | float phi = c * c; 20 | float K = c / newQ; 21 | float a0 = phi + K + 1.0f; 22 | 23 | b[0] = phi / a0; 24 | b[1] = -2.0f * b[0]; 25 | b[2] = b[0]; 26 | a[1] = 2.0f * (1.0f - phi) / a0; 27 | a[2] = (phi - K + 1.0f) / a0; 28 | } 29 | 30 | private: 31 | 32 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HPF2) 33 | }; 34 | 35 | #endif // HPF_H_INCLUDED 36 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/LPF.h: -------------------------------------------------------------------------------- 1 | #ifndef LPF_H_INCLUDED 2 | #define LPF_H_INCLUDED 3 | 4 | #include "Biquad.h" 5 | 6 | class LPF2 : public Biquad 7 | { 8 | public: 9 | LPF2() {} 10 | 11 | void calcCoefs (float newFc, float newQ, float newGainDB = 0.0f) override 12 | { 13 | fc = newFc; 14 | Q = newQ; 15 | gainDB = newGainDB; 16 | 17 | float wc = MathConstants::twoPi * fc / fs; 18 | float c = 1.0f / tan (wc / 2.0f); 19 | float phi = c * c; 20 | float K = c / newQ; 21 | float a0 = phi + K + 1.0f; 22 | 23 | b[0] = 1.0f / a0; 24 | b[1] = 2.0f * b[0]; 25 | b[2] = b[0]; 26 | a[1] = 2.0f * (1.0f - phi) / a0; 27 | a[2] = (phi - K + 1.0f) / a0; 28 | } 29 | 30 | private: 31 | 32 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LPF2) 33 | }; 34 | 35 | #endif // LPF_H_INCLUDED 36 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/ModeBandpass.cpp: -------------------------------------------------------------------------------- 1 | #include "ModeBandpass.h" 2 | 3 | namespace 4 | { 5 | constexpr float butterQs[2] = { 0.5412f, 1.3065f }; 6 | } 7 | 8 | ModeBandpass::ModeBandpass (float fc, float bandwidth, double fs) 9 | { 10 | float highFreq = jmin (fc + bandwidth / 2.0f, (float) fs / 2.0f - 50.0f); 11 | float lowFreq = jmax (fc - bandwidth / 2.0f, 10.0f); 12 | 13 | for (int ch = 0; ch < 2; ++ch) 14 | { 15 | for (int i = 0; i < 2; ++i) 16 | { 17 | lpfs[ch][i].reset (fs); 18 | lpfs[ch][i].calcCoefs (highFreq, butterQs[i]); 19 | 20 | hpfs[ch][i].reset (fs); 21 | hpfs[ch][i].calcCoefs (lowFreq, butterQs[i]); 22 | } 23 | } 24 | } 25 | 26 | void ModeBandpass::processBlock (AudioBuffer& buffer) 27 | { 28 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 29 | { 30 | auto* x = buffer.getWritePointer (ch); 31 | 32 | for (int i = 0; i < 2; ++i) 33 | { 34 | lpfs[ch][i].processBlock (x, buffer.getNumSamples()); 35 | hpfs[ch][i].processBlock (x, buffer.getNumSamples()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/Filters/ModeBandpass.h: -------------------------------------------------------------------------------- 1 | #ifndef MODEBANDPASS_H_INCLUDED 2 | #define MODEBANDPASS_H_INCLUDED 3 | 4 | #include "LPF.h" 5 | #include "HPF.h" 6 | 7 | /** 8 | 4th order Butterworth BPF 9 | Centered around a mode, with some desired bandwidth 10 | */ 11 | class ModeBandpass 12 | { 13 | public: 14 | ModeBandpass (float fc, float bandwidth, double fs); 15 | 16 | void processBlock (AudioBuffer& buffer); 17 | 18 | private: 19 | LPF2 lpfs[2][2]; 20 | HPF2 hpfs[2][2]; 21 | 22 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModeBandpass) 23 | }; 24 | 25 | #endif // MODEBANDPASS_H_INCLUDED 26 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/BaseDetector.cpp: -------------------------------------------------------------------------------- 1 | #include "BaseDetector.h" 2 | 3 | void BaseDetector::setAttackMs (float newAttackMs) 4 | { 5 | if (attackMs != newAttackMs) 6 | { 7 | attackMs = newAttackMs; 8 | a1_a = exp (-1.0f / (fs * attackMs / 1000.0f)); 9 | b0_a = 1.0f - a1_a; 10 | } 11 | } 12 | 13 | void BaseDetector::setReleaseMs (float newReleaseMs) 14 | { 15 | if (releaseMs != newReleaseMs) 16 | { 17 | releaseMs = newReleaseMs; 18 | a1_r = exp (-1.0f / (fs * releaseMs / 1000.0f)); 19 | b0_r = 1.0f - a1_r; 20 | } 21 | } 22 | 23 | void BaseDetector::reset (float sampleRate) 24 | { 25 | fs = sampleRate; 26 | levelEst = 0.0f; 27 | 28 | setAttackMs (attackMs); 29 | setReleaseMs (releaseMs); 30 | } 31 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/BaseDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef BASEDETECTOR_H_INCLUDED 2 | #define BASEDETECTOR_H_INCLUDED 3 | 4 | #include "JuceHeader.h" 5 | 6 | class BaseDetector 7 | { 8 | public: 9 | BaseDetector() {} 10 | virtual ~BaseDetector() {} 11 | 12 | virtual void setAttackMs (float newAttackMs); 13 | virtual void setReleaseMs (float newReleaseMs); 14 | 15 | void reset (float sampleRate); 16 | virtual inline float process (float /*x*/) = 0; 17 | 18 | protected: 19 | // Attack coefs 20 | float attackMs = 1.0f; 21 | float a1_a = 0.0f; 22 | float b0_a = 1.0f; 23 | 24 | // Release coefs 25 | float releaseMs = 20.0f; 26 | float a1_r = 0.0f; 27 | float b0_r = 1.0f; 28 | 29 | float levelEst = 0.0f; 30 | 31 | private: 32 | float fs = 48000.0f; 33 | 34 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BaseDetector) 35 | }; 36 | 37 | #endif // BASEDETECTOR_H_INCLUDED 38 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/PeakDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef PEAKDETECTOR_H_INCLUDED 2 | #define PEAKDETECTOR_H_INCLUDED 3 | 4 | #include "BaseDetector.h" 5 | 6 | class PeakDetector : public BaseDetector 7 | { 8 | public: 9 | PeakDetector() {} 10 | 11 | inline float process (float x) override 12 | { 13 | auto xAbs = abs (x); 14 | if (xAbs > levelEst) 15 | levelEst += b0_a * (xAbs - levelEst); 16 | else 17 | levelEst += b0_r * (xAbs - levelEst); 18 | 19 | return levelEst; 20 | } 21 | 22 | private: 23 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PeakDetector) 24 | }; 25 | 26 | #endif // PEAKDETECTOR_H_INCLUDED 27 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/RMSDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef RMSDETECTOR_H_INCLUDED 2 | #define RMSDETECTOR_H_INCLUDED 3 | 4 | #include "BaseDetector.h" 5 | 6 | class RMSDetector : public BaseDetector 7 | { 8 | public: 9 | RMSDetector() {} 10 | 11 | inline float process (float x) override 12 | { 13 | auto input = x * x; 14 | auto outputSquare = 0.0f; 15 | 16 | if (input > levelEst) 17 | outputSquare = a1_a * levelEst + b0_a * input; 18 | else 19 | outputSquare = a1_r * levelEst + b0_r * input; 20 | 21 | levelEst = outputSquare; 22 | return sqrt (outputSquare); 23 | } 24 | 25 | void processBlock (float* buffer, int numSamples) 26 | { 27 | for (int n = 0; n < numSamples; ++n) 28 | buffer[n] = process (buffer[n]); 29 | } 30 | 31 | private: 32 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RMSDetector) 33 | }; 34 | 35 | #endif // RMSDETECTOR_H_INCLUDED 36 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/TransientDetector.cpp: -------------------------------------------------------------------------------- 1 | #include "TransientDetector.h" 2 | 3 | void TransientDetector::prepare (double sampleRate, int nChannels, int nSamples) 4 | { 5 | for (int ch = 0; ch < 2; ++ch) 6 | { 7 | pDetect[ch].reset ((float) sampleRate); 8 | pDetect[ch].setAttackMs (0.1f); 9 | pDetect[ch].setReleaseMs (150.0f); 10 | } 11 | 12 | detectBuffer.setSize (nChannels, nSamples); 13 | } 14 | 15 | void TransientDetector::resetTransient() 16 | { 17 | count = countToWait; 18 | noiseFloor = Decibels::decibelsToGain (0.0f); 19 | } 20 | 21 | int TransientDetector::isTransientStarting (const AudioBuffer& buffer) 22 | { 23 | if (! isTransientInBuffer (buffer)) 24 | { 25 | inTransient = false; 26 | return -1; 27 | } 28 | 29 | if (inTransient) 30 | return -1; 31 | 32 | inTransient = true; 33 | 34 | // find argmax 35 | auto getMaxValueAtIndex = [=] (int idx) 36 | { 37 | float value = 0.0f; 38 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 39 | { 40 | auto newVal = abs (detectBuffer.getSample (ch, idx)); 41 | if (newVal > value) 42 | value = newVal; 43 | } 44 | 45 | return value; 46 | }; 47 | 48 | int maxSample = 0; 49 | float maxVal = getMaxValueAtIndex (maxSample); 50 | for (int n = 1; n < buffer.getNumSamples(); ++n) 51 | { 52 | auto newMaxVal = getMaxValueAtIndex (n); 53 | if (newMaxVal > maxVal) 54 | { 55 | maxVal = newMaxVal; 56 | maxSample = n; 57 | } 58 | } 59 | 60 | return maxSample; 61 | } 62 | 63 | bool TransientDetector::isTransientInBuffer (const AudioBuffer& buffer) 64 | { 65 | processDetectBuffer (buffer); 66 | 67 | if (detectBuffer.getMagnitude (0, buffer.getNumSamples()) > 5*noiseFloor) 68 | { 69 | count = 0; 70 | return true; 71 | } 72 | 73 | // update noise floor (moving avg of rms values) 74 | float rms = 0.01f; 75 | for (int ch = 0; ch < detectBuffer.getNumChannels(); ++ch) 76 | rms = jmax (rms, detectBuffer.getRMSLevel (ch, 0, buffer.getNumSamples())); 77 | 78 | noiseFloor -= noiseFloor / (float) movingAvgN; 79 | noiseFloor += rms / (float) movingAvgN; 80 | 81 | // if still in wait buffer, return true 82 | if (count < countToWait) 83 | { 84 | count += buffer.getNumSamples(); 85 | return true; 86 | } 87 | 88 | return false; 89 | } 90 | 91 | void TransientDetector::processDetectBuffer (const AudioBuffer& buffer) 92 | { 93 | detectBuffer.makeCopyOf (buffer, true); 94 | 95 | for (int ch = 0; ch < detectBuffer.getNumChannels(); ++ch) 96 | { 97 | auto x = detectBuffer.getWritePointer (ch); 98 | for (int n = 0; n < buffer.getNumSamples(); ++n) 99 | x[n] = pDetect[ch].process (x[n]); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /DrumFixer/Source/DSP/LevelDetectors/TransientDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef TRANSIENTDETECTOR_H_INCLUDED 2 | #define TRANSIENTDETECTOR_H_INCLUDED 3 | 4 | #include "PeakDetector.h" 5 | 6 | class TransientDetector 7 | { 8 | public: 9 | TransientDetector() {} 10 | 11 | void prepare (double sampleRate, int nChannels, int nSamples); 12 | void resetTransient(); 13 | 14 | int isTransientStarting (const AudioBuffer& buffer); 15 | bool isTransientInBuffer (const AudioBuffer& buffer); 16 | 17 | void processDetectBuffer (const AudioBuffer& buffer); 18 | 19 | private: 20 | AudioBuffer detectBuffer; 21 | PeakDetector pDetect[2]; 22 | 23 | float noiseFloor = Decibels::decibelsToGain (0.0f); 24 | const int movingAvgN = 20; 25 | 26 | const int countToWait = 2048; 27 | int count = countToWait; 28 | 29 | bool inTransient = false; 30 | 31 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TransientDetector) 32 | }; 33 | 34 | #endif // TRANSIENTDETECTOR_H_INCLUDED 35 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/FilterList.cpp: -------------------------------------------------------------------------------- 1 | #include "FilterList.h" 2 | 3 | FilterListHeader::FilterListHeader() 4 | { 5 | addAndMakeVisible (freqLabel); 6 | freqLabel.setText ("Center Freq.", dontSendNotification); 7 | 8 | addAndMakeVisible (bwLabel); 9 | bwLabel.setText ("Bandwidth", dontSendNotification); 10 | 11 | addAndMakeVisible (tauLabel); 12 | tauLabel.setText ("Desired T60", dontSendNotification); 13 | 14 | setSize (100, 30); 15 | } 16 | 17 | void FilterListHeader::resized() 18 | { 19 | freqLabel.setBounds (0, 0, 3 * getWidth() / 10, getHeight()); 20 | bwLabel.setBounds (freqLabel.getRight(), 0, 3 * getWidth() / 10, getHeight()); 21 | tauLabel.setBounds (bwLabel.getRight(), 0, 3 * getWidth() / 10, getHeight()); 22 | } 23 | 24 | void FilterListHeader::paint (Graphics& g) 25 | { 26 | g.fillAll (Colours::black); 27 | 28 | g.setColour (Colours::white); 29 | const auto y = (float) getHeight() - 2.0f; 30 | g.drawLine (0.0f, y, (float) getWidth(), y); 31 | } 32 | 33 | //================================================== 34 | FilterListComp::FilterListComp (DecayFilter& filt, FilterList* parentList) : 35 | filt (filt) 36 | { 37 | auto setupLabel = [=, &filt] (Label& label, float& value) 38 | { 39 | addAndMakeVisible (label); 40 | label.setEditable (true); 41 | label.onTextChange = [=, &value, &label, &filt] 42 | { 43 | value = label.getText().getFloatValue(); 44 | filt.updateFilter(); 45 | parentList->sendChangeMessage(); 46 | updateLabels(); 47 | }; 48 | }; 49 | 50 | auto& params = filt.getParams(); 51 | setupLabel (freqLabel, params.centerFreq); 52 | setupLabel (bwLabel, params.bandwidth); 53 | setupLabel (tauLabel, params.desiredT60); 54 | 55 | updateLabels(); 56 | 57 | setInterceptsMouseClicks (false, true); 58 | } 59 | 60 | void FilterListComp::updateLabels() 61 | { 62 | const auto& params = filt.getParams(); 63 | freqLabel.setText (String (params.centerFreq, 2) + " Hz", dontSendNotification); 64 | bwLabel.setText (String (params.bandwidth , 2) + " Hz", dontSendNotification); 65 | tauLabel.setText (String (params.desiredT60, 4) + " sec", dontSendNotification); 66 | } 67 | 68 | void FilterListComp::resized() 69 | { 70 | freqLabel.setBounds (0, 0, 3 * getWidth() / 10, getHeight()); 71 | bwLabel.setBounds (freqLabel.getRight(), 0, 3 * getWidth() / 10, getHeight()); 72 | tauLabel.setBounds (bwLabel.getRight(), 0, 3 * getWidth() / 10, getHeight()); 73 | } 74 | 75 | //================================================== 76 | FilterList::FilterList (DrumFixerAudioProcessor& p) : 77 | ListBox (String(), this), 78 | proc (p) 79 | { 80 | setHeaderComponent (new FilterListHeader); 81 | setRowSelectedOnMouseDown (true); 82 | setColour (ListBox::backgroundColourId, Colours::black); 83 | } 84 | 85 | void FilterList::paintListBoxItem (int /*row*/, Graphics& g, int width, int /*height*/, bool isSelected) 86 | { 87 | if (isSelected) 88 | g.fillAll (Colours::green); 89 | else 90 | g.fillAll (Colours::black); 91 | 92 | g.setColour (Colours::green.darker (isSelected ? 0.8f : 0.0f)); 93 | g.fillRect ((float) width - 20.0f, 10.0f, 10.0f, 10.0f); 94 | } 95 | 96 | Component* FilterList::refreshComponentForRow (int rowNum, bool /*isSelected*/, Component* comp) 97 | { 98 | if (rowNum >= proc.getDecayFilters().size()) 99 | { 100 | delete comp; 101 | return nullptr; 102 | } 103 | 104 | auto compCast = dynamic_cast (comp); 105 | 106 | if (compCast != nullptr) // try to use existing component... 107 | { 108 | if (proc.getDecayFilters()[rowNum] == &compCast->getFilt()) 109 | return comp; 110 | 111 | delete compCast; 112 | } 113 | 114 | // create new component 115 | auto filt = proc.getDecayFilters()[rowNum]; 116 | return new FilterListComp (*filt, this); 117 | } 118 | 119 | void FilterList::deleteKeyPressed (int lastSelectedRow) 120 | { 121 | if (lastSelectedRow >= proc.getDecayFilters().size()) 122 | return; 123 | 124 | proc.getDecayFilters().remove (lastSelectedRow, true); 125 | updateContent(); 126 | sendChangeMessage(); 127 | } 128 | 129 | void FilterList::selectedRowsChanged (int lastSelected) 130 | { 131 | auto& filts = proc.getDecayFilters(); 132 | for (int i = 0; i < filts.size(); ++i) 133 | filts[i]->setSelected (i == lastSelected); 134 | 135 | getParentComponent()->repaint(); 136 | } 137 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/FilterList.h: -------------------------------------------------------------------------------- 1 | #ifndef FILTERLIST_H_INCLUDED 2 | #define FILTERLIST_H_INCLUDED 3 | 4 | #include "../PluginProcessor.h" 5 | 6 | class FilterList : public ListBox, 7 | public ListBoxModel, 8 | public ChangeBroadcaster 9 | { 10 | public: 11 | FilterList (DrumFixerAudioProcessor& p); 12 | 13 | int getNumRows() override { return proc.getDecayFilters().size(); } 14 | void paintListBoxItem (int row, Graphics& g, int width, int height, bool isSelected) override; 15 | Component* refreshComponentForRow (int rowNum, bool isSelected, Component* comp) override; 16 | void deleteKeyPressed (int lastSelectedRow) override; 17 | void selectedRowsChanged (int lastRowSelected) override; 18 | 19 | private: 20 | DrumFixerAudioProcessor& proc; 21 | 22 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilterList) 23 | }; 24 | 25 | class FilterListComp : public Component 26 | { 27 | public: 28 | FilterListComp (DecayFilter& filt, FilterList* parentList); 29 | 30 | void updateLabels(); 31 | void resized() override; 32 | 33 | DecayFilter& getFilt() { return filt; } 34 | 35 | private: 36 | DecayFilter& filt; 37 | 38 | Label freqLabel; 39 | Label bwLabel; 40 | Label tauLabel; 41 | 42 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilterListComp) 43 | }; 44 | 45 | class FilterListHeader : public Component 46 | { 47 | public: 48 | FilterListHeader (); 49 | 50 | void resized() override; 51 | void paint (Graphics& g) override; 52 | 53 | private: 54 | Label freqLabel; 55 | Label bwLabel; 56 | Label tauLabel; 57 | 58 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilterListHeader) 59 | }; 60 | 61 | #endif // FILTERLIST_H_INCLUDED 62 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/SpectrogramComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "SpectrogramComponent.h" 2 | 3 | SpectrogramComponent::SpectrogramComponent (FFTUtils& fft) : 4 | fft (fft) 5 | { 6 | setOpaque (true); 7 | startTimerHz (45); 8 | } 9 | 10 | void SpectrogramComponent::timerCallback() 11 | { 12 | if (fft.isFFTReady()) 13 | { 14 | Array spectrogramLine; 15 | fft.drawNextLineOfSpectrogram (spectrogramLine, specgramPixels); 16 | specgramData.add (spectrogramLine); 17 | } 18 | } 19 | 20 | void SpectrogramComponent::clear() 21 | { 22 | spectrogramImage.reset (nullptr); 23 | specgramData.clear(); 24 | repaint(); 25 | } 26 | 27 | void SpectrogramComponent::drawSpecgram() 28 | { 29 | if (specgramData.size() == 0) 30 | return; 31 | 32 | spectrogramImage = std::make_unique (Image::RGB, specgramData.size(), specgramPixels, true); 33 | for (int x = 0; x < specgramData.size(); ++x) 34 | for (int y = 0; y < specgramPixels; ++y) 35 | spectrogramImage->setPixelAt (x, y, specgramData[x][y]); 36 | 37 | repaint(); 38 | } 39 | 40 | void SpectrogramComponent::paint (Graphics& g) 41 | { 42 | g.fillAll (Colours::black); 43 | 44 | g.setOpacity (1.0f); 45 | if (spectrogramImage.get() != nullptr) 46 | g.drawImage (*spectrogramImage.get(), getLocalBounds().toFloat()); 47 | } 48 | 49 | Image SpectrogramComponent::getImageCopy() 50 | { 51 | if (spectrogramImage.get() == nullptr) 52 | return Image(); 53 | 54 | return spectrogramImage->createCopy(); 55 | } 56 | 57 | void SpectrogramComponent::setImage (Image image) 58 | { 59 | if (image.isValid()) 60 | spectrogramImage = std::make_unique (image); 61 | } 62 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/SpectrogramComponent.h: -------------------------------------------------------------------------------- 1 | #ifndef SPECTROGRAMCOMPONENT_H_INCLUDED 2 | #define SPECTROGRAMCOMPONENT_H_INCLUDED 3 | 4 | #include "../DSP/FFTUtils.h" 5 | 6 | class SpectrogramComponent : public Component, 7 | private Timer 8 | { 9 | public: 10 | SpectrogramComponent (FFTUtils& fft); 11 | 12 | void paint (Graphics& g) override; 13 | void clear(); 14 | void drawSpecgram(); 15 | 16 | Image getImageCopy(); 17 | void setImage (Image image); 18 | 19 | private: 20 | void timerCallback() override; 21 | 22 | enum 23 | { 24 | specgramPixels = 512, 25 | }; 26 | 27 | FFTUtils& fft; 28 | std::unique_ptr spectrogramImage; 29 | Array> specgramData; 30 | 31 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpectrogramComponent) 32 | }; 33 | 34 | #endif // SPECTROGRAMCOMPONENT_H_INCLUDED 35 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/SpectrogramOverlay.cpp: -------------------------------------------------------------------------------- 1 | #include "SpectrogramOverlay.h" 2 | #include "../DSP/FFTUtils.h" 3 | 4 | SpectrogramOverlay::SpectrogramOverlay (DrumFixerAudioProcessor& p) : 5 | proc (p) 6 | { 7 | addChildComponent (cursorLabel); 8 | cursorLabel.setFont (12.0f); 9 | cursorLabel.setJustificationType (Justification::right); 10 | } 11 | 12 | void SpectrogramOverlay::drawFreqLine (Graphics& g, float y, float dim) 13 | { 14 | const auto w = (float) getWidth(); 15 | g.drawLine (0.0f, y, w, y, 2.0f); 16 | 17 | Path lTri; 18 | lTri.addTriangle (0.0f, y + dim, 0.0f, y - dim, dim, y); 19 | g.fillPath (lTri); 20 | 21 | Path rTri; 22 | rTri.addTriangle (w, y + dim, w, y - dim, w - dim, y); 23 | g.fillPath (rTri); 24 | } 25 | 26 | void SpectrogramOverlay::drawWidthLine (Graphics& g, float top, float center, float x) 27 | { 28 | const auto height = 2 * (center - top); 29 | g.drawLine (x, top, x, top + height, 2.0f); 30 | 31 | const float dim = 5.0f; 32 | g.drawLine (x - dim, top, x + dim, top, 1.0f); 33 | g.drawLine (x - dim, top + height, x + dim, top + height, 1.0f); 34 | } 35 | 36 | void SpectrogramOverlay::drawCursor (Graphics& g) 37 | { 38 | g.setColour (Colour (0xff04d9ff)); 39 | if (state == ChooseFreq) 40 | { 41 | if (mouseY > 0.0f) 42 | drawFreqLine (g, mouseY); 43 | } 44 | else if (state == ChooseWidth) 45 | { 46 | drawFreqLine (g, freqY); 47 | 48 | if (mouseY < freqY && mouseY > 0.0f) 49 | drawWidthLine (g, mouseY, freqY, (float) getWidth() / 2); 50 | } 51 | else if (state == ChooseTau) 52 | { 53 | drawFreqLine (g, freqY); 54 | 55 | if (mouseX > 0.0f) 56 | drawWidthLine (g, widthY, freqY, mouseX); 57 | else 58 | drawWidthLine (g, widthY, freqY, (float) getWidth() / 2); 59 | } 60 | } 61 | 62 | void SpectrogramOverlay::drawFilters (Graphics& g) 63 | { 64 | auto& filters = proc.getDecayFilters(); 65 | for (auto filt : filters) 66 | { 67 | g.setColour (filt->isSelected() ? Colours::green.brighter() : Colour (0xff04d9ff).darker (0.4f)); 68 | 69 | auto& params = filt->getParams(); 70 | auto filtY = (float) FFTUtils::freqToY (params.centerFreq, (float) getHeight()); 71 | auto tauX = getXForTau (params.desiredT60); 72 | 73 | drawFreqLine (g, filtY); 74 | drawWidthLine (g, getYForWidth (params.bandwidth, params.centerFreq), filtY, tauX); 75 | } 76 | } 77 | 78 | void SpectrogramOverlay::paint (Graphics& g) 79 | { 80 | drawFilters (g); 81 | drawCursor (g); 82 | 83 | g.setColour (Colours::white); 84 | g.drawRect (getLocalBounds().toFloat(), 2.0f); 85 | } 86 | 87 | void SpectrogramOverlay::mouseMove (const MouseEvent& e) 88 | { 89 | cursorLabel.setVisible (true); 90 | mouseX = (float) e.x; 91 | mouseY = (float) e.y; 92 | updateLabel(); 93 | repaint(); 94 | } 95 | 96 | void SpectrogramOverlay::mouseExit (const MouseEvent& /*e*/) 97 | { 98 | mouseX = -1.0f; 99 | mouseY = -1.0f; 100 | cursorLabel.setVisible (false); 101 | repaint(); 102 | } 103 | 104 | void SpectrogramOverlay::mouseUp (const MouseEvent& e) 105 | { 106 | switch (state) 107 | { 108 | case ChooseFreq: 109 | freqY = (float) e.y; 110 | state = ChooseWidth; 111 | break; 112 | 113 | case ChooseWidth: 114 | widthY = (float) e.y; 115 | state = ChooseTau; 116 | break; 117 | 118 | case ChooseTau: 119 | createNewFilter(); 120 | state = ChooseFreq; 121 | break; 122 | } 123 | 124 | updateLabel(); 125 | repaint(); 126 | } 127 | 128 | float SpectrogramOverlay::getTauForX (float x) 129 | { 130 | return 2.0f * pow (x / (float) getWidth(), 2.0f); 131 | } 132 | 133 | float SpectrogramOverlay::getXForTau (float tau) 134 | { 135 | return pow (tau / 2.0f, 1.0f / 2.0f) * getWidth(); 136 | } 137 | 138 | float SpectrogramOverlay::getWidthForY (float y, float center) 139 | { 140 | return jmax (FFTUtils::yToFreq (y, (float) getHeight()) 141 | - FFTUtils::yToFreq (center, (float) getHeight()), 0.0f); 142 | } 143 | 144 | float SpectrogramOverlay::getYForWidth (float bandwidth, float centerFreq) 145 | { 146 | return (float) FFTUtils::freqToY((bandwidth) + centerFreq, (float) getHeight()); 147 | } 148 | 149 | void SpectrogramOverlay::updateLabel() 150 | { 151 | if (state == ChooseFreq) 152 | { 153 | auto freqHz = FFTUtils::yToFreq (mouseY, (float) getHeight()); 154 | cursorLabel.setText (String (freqHz, 2) + " Hz", dontSendNotification); 155 | 156 | const int width = 100; 157 | const int height = 30; 158 | cursorLabel.setBounds (getWidth() - width, (int) mouseY - height, width, height); 159 | } 160 | 161 | if (state == ChooseWidth) 162 | { 163 | auto widthHz = getWidthForY (mouseY, freqY); 164 | cursorLabel.setText (String (widthHz, 2) + " Hz", dontSendNotification); 165 | 166 | const int width = 100; 167 | const int height = 30; 168 | cursorLabel.setBounds (getWidth() - width, (int) freqY - height, width, height); 169 | } 170 | 171 | if (state == ChooseTau) 172 | { 173 | auto tauSeconds = getTauForX (mouseX); 174 | 175 | cursorLabel.setText (String (tauSeconds, 2) + " sec", dontSendNotification); 176 | 177 | const int width = 100; 178 | const int height = 30; 179 | cursorLabel.setBounds (getWidth() - width, (int) freqY - height, width, height); 180 | } 181 | } 182 | 183 | void SpectrogramOverlay::createNewFilter() 184 | { 185 | auto freqHz = FFTUtils::yToFreq (freqY, (float) getHeight()); 186 | auto widthHz = getWidthForY (mouseY, freqY); 187 | auto tauSeconds = getTauForX (mouseX); 188 | 189 | DecayFilter::Params params (freqHz, widthHz, tauSeconds); 190 | proc.addDecayFilter (params); 191 | sendChangeMessage(); 192 | } 193 | -------------------------------------------------------------------------------- /DrumFixer/Source/GUI/SpectrogramOverlay.h: -------------------------------------------------------------------------------- 1 | #ifndef SPECTROGRAMOVERLAY_H_INCLUDED 2 | #define SPECTROGRAMOVERLAY_H_INCLUDED 3 | 4 | #include "../DSP/DecayFilter.h" 5 | #include "../PluginProcessor.h" 6 | 7 | class SpectrogramOverlay : public Component, 8 | public ChangeBroadcaster 9 | { 10 | public: 11 | SpectrogramOverlay (DrumFixerAudioProcessor& p); 12 | 13 | void paint (Graphics& g) override; 14 | 15 | void mouseMove (const MouseEvent& e) override; 16 | void mouseUp (const MouseEvent& e) override; 17 | void mouseExit (const MouseEvent& e) override; 18 | 19 | void updateLabel(); 20 | void createNewFilter(); 21 | 22 | private: 23 | void drawFreqLine (Graphics& g, float y, float dim = 6.0f); 24 | void drawWidthLine (Graphics& g, float top, float center, float x); 25 | 26 | void drawCursor (Graphics& g); 27 | void drawFilters (Graphics& g); 28 | 29 | float getTauForX (float x); 30 | float getXForTau (float tau); 31 | 32 | float getWidthForY (float y, float center); 33 | float getYForWidth (float bandwidth, float centerFreq); 34 | 35 | DrumFixerAudioProcessor& proc; 36 | 37 | Label cursorLabel; 38 | float mouseY = -1.0f; 39 | float mouseX = -1.0f; 40 | 41 | float freqY; 42 | float widthY; 43 | 44 | enum State 45 | { 46 | ChooseFreq, 47 | ChooseWidth, 48 | ChooseTau 49 | }; 50 | 51 | State state = ChooseFreq; 52 | 53 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpectrogramOverlay) 54 | }; 55 | 56 | #endif // SPECTROGRAMOVERLAY_H_INCLUDED 57 | -------------------------------------------------------------------------------- /DrumFixer/Source/PluginEditor.cpp: -------------------------------------------------------------------------------- 1 | #include "PluginProcessor.h" 2 | #include "PluginEditor.h" 3 | 4 | DrumFixerAudioProcessorEditor::DrumFixerAudioProcessorEditor (DrumFixerAudioProcessor& p) : 5 | AudioProcessorEditor (&p), 6 | processor (p), 7 | specgram (p.getFFT()), 8 | overlay (p), 9 | filterList (p) 10 | { 11 | setSize (650, 450); 12 | 13 | processor.addChangeListener (this); 14 | 15 | addAndMakeVisible (specgram); 16 | 17 | addAndMakeVisible (overlay); 18 | overlay.setMouseCursor (MouseCursor::NoCursor); 19 | overlay.addChangeListener (this); 20 | 21 | addAndMakeVisible (filterList); 22 | filterList.addChangeListener (this); 23 | 24 | addAndMakeVisible (listenButton); 25 | listenButton.onClick = [=] { listenButtonPressed(); }; 26 | 27 | reload(); 28 | } 29 | 30 | DrumFixerAudioProcessorEditor::~DrumFixerAudioProcessorEditor() 31 | { 32 | getSpectrogramImage(); 33 | } 34 | 35 | void DrumFixerAudioProcessorEditor::reload() 36 | { 37 | specgram.setImage (processor.specgramImage); 38 | filterList.updateContent(); 39 | repaint(); 40 | } 41 | 42 | void DrumFixerAudioProcessorEditor::getSpectrogramImage() 43 | { 44 | processor.specgramImage = specgram.getImageCopy(); 45 | } 46 | 47 | void DrumFixerAudioProcessorEditor::paint (Graphics& g) 48 | { 49 | g.fillAll (Colours::black); 50 | 51 | auto& fftUtils = processor.getFFT(); 52 | g.setGradientFill (ColourGradient (fftUtils.getInfernoColour (0.88f), 5, 5, 53 | fftUtils.getInfernoColour (0.25f), 250, 50, false)); 54 | 55 | GlyphArrangement ga; 56 | ga.addLineOfText (Font (45.0f).boldened(), "DrumFixer", 5, 40); 57 | Path p; 58 | ga.createPath (p); 59 | g.fillPath (p); 60 | } 61 | 62 | void DrumFixerAudioProcessorEditor::resized() 63 | { 64 | const int pad = 10; 65 | filterList.setBounds (pad, 100, 240, getHeight() - 50 - 2*pad); 66 | filterList.setRowHeight (30); 67 | 68 | listenButton.setBounds (75, 60, 100, 30); 69 | 70 | specgram.setBounds (260, pad, 400 - 2*pad, getHeight() - 2*pad); 71 | overlay.setBounds (specgram.getBounds()); 72 | } 73 | 74 | void DrumFixerAudioProcessorEditor::listenButtonPressed() 75 | { 76 | processor.toggleListening(); 77 | } 78 | 79 | void DrumFixerAudioProcessorEditor::changeListenerCallback (ChangeBroadcaster* source) 80 | { 81 | if (source == &processor) 82 | { 83 | if (processor.isListening()) // started listening 84 | { 85 | specgram.clear(); 86 | listenButton.setButtonText ("Stop"); 87 | } 88 | else // stopped listening 89 | { 90 | specgram.drawSpecgram(); 91 | listenButton.setButtonText ("Listen"); 92 | } 93 | } 94 | 95 | if (source == &overlay) 96 | { 97 | filterList.updateContent(); 98 | } 99 | 100 | if (source == &filterList) 101 | { 102 | overlay.repaint(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /DrumFixer/Source/PluginEditor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../JuceLibraryCode/JuceHeader.h" 4 | #include "PluginProcessor.h" 5 | #include "GUI/SpectrogramComponent.h" 6 | #include "GUI/SpectrogramOverlay.h" 7 | #include "GUI/FilterList.h" 8 | 9 | class DrumFixerAudioProcessorEditor : public AudioProcessorEditor, 10 | private ChangeListener 11 | { 12 | public: 13 | DrumFixerAudioProcessorEditor (DrumFixerAudioProcessor&); 14 | ~DrumFixerAudioProcessorEditor(); 15 | 16 | void paint (Graphics&) override; 17 | void resized() override; 18 | 19 | void reload(); 20 | void getSpectrogramImage(); 21 | 22 | void listenButtonPressed(); 23 | 24 | void changeListenerCallback (ChangeBroadcaster* source); 25 | 26 | private: 27 | DrumFixerAudioProcessor& processor; 28 | 29 | SpectrogramComponent specgram; 30 | SpectrogramOverlay overlay; 31 | FilterList filterList; 32 | 33 | TextButton listenButton { "Listen" }; 34 | 35 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrumFixerAudioProcessorEditor) 36 | }; 37 | -------------------------------------------------------------------------------- /DrumFixer/Source/PluginProcessor.cpp: -------------------------------------------------------------------------------- 1 | #include "PluginProcessor.h" 2 | #include "PluginEditor.h" 3 | 4 | DrumFixerAudioProcessor::DrumFixerAudioProcessor() 5 | #ifndef JucePlugin_PreferredChannelConfigurations 6 | : AudioProcessor (BusesProperties() 7 | #if ! JucePlugin_IsMidiEffect 8 | #if ! JucePlugin_IsSynth 9 | .withInput ("Input", AudioChannelSet::stereo(), true) 10 | #endif 11 | .withOutput ("Output", AudioChannelSet::stereo(), true) 12 | #endif 13 | ) 14 | #endif 15 | { 16 | } 17 | 18 | DrumFixerAudioProcessor::~DrumFixerAudioProcessor() 19 | { 20 | } 21 | 22 | const String DrumFixerAudioProcessor::getName() const 23 | { 24 | return JucePlugin_Name; 25 | } 26 | 27 | bool DrumFixerAudioProcessor::acceptsMidi() const 28 | { 29 | #if JucePlugin_WantsMidiInput 30 | return true; 31 | #else 32 | return false; 33 | #endif 34 | } 35 | 36 | bool DrumFixerAudioProcessor::producesMidi() const 37 | { 38 | #if JucePlugin_ProducesMidiOutput 39 | return true; 40 | #else 41 | return false; 42 | #endif 43 | } 44 | 45 | bool DrumFixerAudioProcessor::isMidiEffect() const 46 | { 47 | #if JucePlugin_IsMidiEffect 48 | return true; 49 | #else 50 | return false; 51 | #endif 52 | } 53 | 54 | double DrumFixerAudioProcessor::getTailLengthSeconds() const 55 | { 56 | return 0.0; 57 | } 58 | 59 | int DrumFixerAudioProcessor::getNumPrograms() 60 | { 61 | return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs, 62 | // so this should be at least 1, even if you're not really implementing programs. 63 | } 64 | 65 | int DrumFixerAudioProcessor::getCurrentProgram() 66 | { 67 | return 0; 68 | } 69 | 70 | void DrumFixerAudioProcessor::setCurrentProgram (int index) 71 | { 72 | } 73 | 74 | const String DrumFixerAudioProcessor::getProgramName (int index) 75 | { 76 | return {}; 77 | } 78 | 79 | void DrumFixerAudioProcessor::changeProgramName (int index, const String& newName) 80 | { 81 | } 82 | 83 | void DrumFixerAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock) 84 | { 85 | fftUtils.setSampleRate (getSampleRate()); 86 | tDetect.prepare (sampleRate, getMainBusNumInputChannels(), samplesPerBlock); 87 | 88 | for (auto filt : decayFilts) 89 | filt->prepare (sampleRate, samplesPerBlock); 90 | } 91 | 92 | void DrumFixerAudioProcessor::releaseResources() 93 | { 94 | } 95 | 96 | #ifndef JucePlugin_PreferredChannelConfigurations 97 | bool DrumFixerAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const 98 | { 99 | #if JucePlugin_IsMidiEffect 100 | ignoreUnused (layouts); 101 | return true; 102 | #else 103 | // This is the place where you check if the layout is supported. 104 | // In this template code we only support mono or stereo. 105 | if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono() 106 | && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo()) 107 | return false; 108 | 109 | // This checks if the input layout matches the output layout 110 | #if ! JucePlugin_IsSynth 111 | if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet()) 112 | return false; 113 | #endif 114 | 115 | return true; 116 | #endif 117 | } 118 | #endif 119 | 120 | void DrumFixerAudioProcessor::processBlock (AudioBuffer& buffer, MidiBuffer& midiMessages) 121 | { 122 | ScopedNoDenormals noDenormals; 123 | 124 | if (listening) 125 | { 126 | if (tDetect.isTransientInBuffer (buffer)) 127 | { 128 | transDetected = true; 129 | fftUtils.processBlock (buffer); 130 | 131 | auto curTransBufferSize = transientBuffer.getNumSamples(); 132 | transientBuffer.setSize (buffer.getNumChannels(), 133 | curTransBufferSize + buffer.getNumSamples(), true); 134 | 135 | for (int ch = 0; ch < buffer.getNumChannels(); ++ch) 136 | transientBuffer.copyFrom (ch, curTransBufferSize, buffer, ch, 0, buffer.getNumSamples()); 137 | } 138 | else if (transDetected) 139 | { 140 | toggleListening(); 141 | } 142 | } 143 | else 144 | { 145 | int transSample = tDetect.isTransientStarting (buffer); 146 | 147 | if (transSample >= 0) 148 | { 149 | for (auto filt : decayFilts) 150 | filt->newTransient (transSample); 151 | } 152 | 153 | for (auto filt : decayFilts) 154 | filt->processBlock (buffer); 155 | } 156 | } 157 | 158 | void DrumFixerAudioProcessor::toggleListening() 159 | { 160 | if (! listening) // start listening 161 | { 162 | listening = true; 163 | transDetected = false; 164 | 165 | transientBuffer.clear(); 166 | transientBuffer.setSize (getMainBusNumInputChannels(), 0); 167 | 168 | tDetect.resetTransient(); 169 | } 170 | else // stop listening 171 | { 172 | listening = false; 173 | transientSampleRate = getSampleRate(); 174 | 175 | for (auto* filt : decayFilts) 176 | filt->updateFilter(); 177 | } 178 | 179 | sendChangeMessage(); 180 | } 181 | 182 | void DrumFixerAudioProcessor::addDecayFilter (DecayFilter::Params& params) 183 | { 184 | if (transientBuffer.hasBeenCleared()) 185 | return; 186 | 187 | decayFilts.add (new DecayFilter (params, transientBuffer, transientSampleRate)); 188 | decayFilts.getLast()->prepare (getSampleRate(), getBlockSize()); 189 | } 190 | 191 | bool DrumFixerAudioProcessor::hasEditor() const 192 | { 193 | return true; // (change this to false if you choose to not supply an editor) 194 | } 195 | 196 | AudioProcessorEditor* DrumFixerAudioProcessor::createEditor() 197 | { 198 | return new DrumFixerAudioProcessorEditor (*this); 199 | } 200 | 201 | void DrumFixerAudioProcessor::getStateInformation (MemoryBlock& destData) 202 | { 203 | // create root xml element 204 | auto xmlState = std::make_unique ("PluginState"); 205 | 206 | // save spectrogram image to file 207 | if (auto editor = dynamic_cast (getActiveEditor())) 208 | editor->getSpectrogramImage(); 209 | 210 | if (specgramImage.isValid()) 211 | { 212 | File specgramFile = File::getSpecialLocation (File::userApplicationDataDirectory) 213 | .getChildFile ("DrumFixer") 214 | .getChildFile ("Spectrograms") 215 | .getNonexistentChildFile ("specgram", ".png", false); 216 | specgramFile.create(); 217 | 218 | FileOutputStream fStream (specgramFile); 219 | PNGImageFormat png; 220 | png.writeImageToStream (specgramImage, fStream); 221 | xmlState->setAttribute ("Spectrogram", specgramFile.getFullPathName()); 222 | } 223 | 224 | // save transient buffer to file 225 | if (! transientBuffer.hasBeenCleared() && transientBuffer.getNumChannels() > 0) 226 | { 227 | File transientFile = File::getSpecialLocation (File::userApplicationDataDirectory) 228 | .getChildFile ("DrumFixer") 229 | .getChildFile ("Transients") 230 | .getNonexistentChildFile ("transient", ".wav", false); 231 | transientFile.create(); 232 | 233 | WavAudioFormat wav; 234 | std::unique_ptr writer (wav 235 | .createWriterFor (new FileOutputStream (transientFile), transientSampleRate, 236 | transientBuffer.getNumChannels(), 16, {}, 0)); 237 | writer->writeFromAudioSampleBuffer (transientBuffer, 0, transientBuffer.getNumSamples()); 238 | xmlState->setAttribute ("Transient", transientFile.getFullPathName()); 239 | } 240 | 241 | // save decay filters 242 | auto filtersXml = std::make_unique ("Filters"); 243 | int i = 0; 244 | for (auto filt : decayFilts) 245 | { 246 | auto filtXml = filt->toXml(); 247 | filtXml->setTagName ("DecayFilter" + String (i++)); 248 | filtersXml->addChildElement (filtXml.release()); 249 | } 250 | 251 | xmlState->addChildElement (filtersXml.release()); 252 | 253 | // write XML to file 254 | copyXmlToBinary (*xmlState.get(), destData); 255 | } 256 | 257 | void DrumFixerAudioProcessor::setStateInformation (const void* data, int sizeInBytes) 258 | { 259 | // read XML from file 260 | auto xmlState = getXmlFromBinary (data, sizeInBytes); 261 | 262 | // read spectrogram image 263 | if (xmlState->hasAttribute ("Spectrogram")) 264 | { 265 | auto specgramFile = File (xmlState->getStringAttribute ("Spectrogram")); 266 | specgramImage = PNGImageFormat::loadFrom (specgramFile); 267 | } 268 | 269 | // read transient file 270 | if (xmlState->hasAttribute ("Transient")) 271 | { 272 | transientSampleRate = xmlState->getDoubleAttribute ("SampleRate"); 273 | 274 | auto transientFile = File (xmlState->getStringAttribute ("Transient")); 275 | WavAudioFormat wav; 276 | std::unique_ptr reader (wav 277 | .createReaderFor (new FileInputStream (transientFile), true)); 278 | 279 | transientBuffer.setSize (reader->numChannels, (int) reader->lengthInSamples); 280 | reader->read (&transientBuffer, 0, (int) reader->lengthInSamples, 0, true, true); 281 | transientSampleRate = reader->sampleRate; 282 | } 283 | 284 | // load decay filters 285 | decayFilts.clear(); 286 | if (auto filtersXml = xmlState->getChildByName ("Filters")) 287 | { 288 | forEachXmlChildElement (*filtersXml, filtXml) 289 | decayFilts.add (DecayFilter::fromXml (filtXml, transientBuffer, (float) transientSampleRate)); 290 | } 291 | 292 | if (auto editor = dynamic_cast (getActiveEditor())) 293 | editor->reload(); 294 | } 295 | 296 | // This creates new instances of the plugin.. 297 | AudioProcessor* JUCE_CALLTYPE createPluginFilter() 298 | { 299 | return new DrumFixerAudioProcessor(); 300 | } 301 | -------------------------------------------------------------------------------- /DrumFixer/Source/PluginProcessor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../JuceLibraryCode/JuceHeader.h" 4 | #include "DSP/FFTUtils.h" 5 | #include "DSP/LevelDetectors/TransientDetector.h" 6 | #include "DSP/DecayFilter.h" 7 | 8 | class DrumFixerAudioProcessor : public AudioProcessor, 9 | public ChangeBroadcaster 10 | { 11 | public: 12 | DrumFixerAudioProcessor(); 13 | ~DrumFixerAudioProcessor(); 14 | 15 | void prepareToPlay (double sampleRate, int samplesPerBlock) override; 16 | void releaseResources() override; 17 | 18 | #ifndef JucePlugin_PreferredChannelConfigurations 19 | bool isBusesLayoutSupported (const BusesLayout& layouts) const override; 20 | #endif 21 | 22 | void processBlock (AudioBuffer&, MidiBuffer&) override; 23 | 24 | AudioProcessorEditor* createEditor() override; 25 | bool hasEditor() const override; 26 | 27 | const String getName() const override; 28 | 29 | bool acceptsMidi() const override; 30 | bool producesMidi() const override; 31 | bool isMidiEffect() const override; 32 | double getTailLengthSeconds() const override; 33 | 34 | int getNumPrograms() override; 35 | int getCurrentProgram() override; 36 | void setCurrentProgram (int index) override; 37 | const String getProgramName (int index) override; 38 | void changeProgramName (int index, const String& newName) override; 39 | 40 | void getStateInformation (MemoryBlock& destData) override; 41 | void setStateInformation (const void* data, int sizeInBytes) override; 42 | 43 | FFTUtils& getFFT() { return fftUtils; } 44 | 45 | bool isListening() { return listening; } 46 | void toggleListening(); 47 | 48 | void addDecayFilter (DecayFilter::Params& params); 49 | OwnedArray& getDecayFilters() { return decayFilts; } 50 | 51 | Image specgramImage; 52 | 53 | private: 54 | FFTUtils fftUtils; 55 | 56 | TransientDetector tDetect; 57 | bool listening = false; 58 | bool transDetected = false; 59 | AudioBuffer transientBuffer; 60 | double transientSampleRate = 48000.0; 61 | 62 | OwnedArray decayFilts; 63 | 64 | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrumFixerAudioProcessor) 65 | }; 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DrumFixer 2 | 3 | DrumFixer is an audio plugin designed to help mixing engineers 4 | achieve better sounding drums. The basic idea is to damp 5 | frequencies in the drum sound that ring out too long. 6 | 7 | ## Usage 8 | 9 | Place the DrumFixer plugin on a track that contains a single drum 10 | sound. In our example, we will use the plugin on a snare drum 11 | track. Press the listen button, and then play a single drum 12 | hit through the plugin. DrumFixer will record the drum sound, 13 | and display the sound as a spectrogram. 14 | 15 | ![Pic](./Screenshots/Listen.gif) 16 | 17 | Note which frequencies seem to ring out too long. Hover your mouse 18 | over those frequencies on the spectrogram and click to choose a 19 | frequency, bandwidth, and decay time. DrumFixer will then create a 20 | filter that damps the drum sound at that frequency to achieve the 21 | desired decay time. 22 | 23 | ![Pic](./Screenshots/Filters.gif) 24 | 25 | Each time a filter is created, it is added to the filter list to 26 | the left of the spectrogram. From this list, you can edit the filter 27 | characteristics, or delete the filter. 28 | 29 | ![Pic](./Screenshots/Edit.gif) 30 | 31 | ## How it works 32 | 33 | Each time you create a filter in DrumFixer, the plugin analyzes 34 | the recorded drum sound by filtering out the frequencies of the 35 | drum sound that the filter will be affecting. DrumFixer then 36 | estimates the decay characteristic of the drum within that frequency 37 | band, and determines the necessary amount of damping needed at that 38 | frequency to achieve the desired decay time. Finally, every time a 39 | new drum hit is detected passing through the plugin, the filter 40 | dynamically adjusts the gain of the frequencies being processed 41 | by the filter to apply the damping effect to the drum sound. 42 | 43 | ![](./Screenshots/PlotBell.gif) 44 | 45 | For a more in-depth explanation, feel free to checkout this 46 | [`python` Notebook](https://ccrma.stanford.edu/~jatin/Notebooks/DrumFixer.html). 47 | 48 | ## Acknowledgements 49 | 50 | The original idea for this plugin was developed by Mark Rau, and 51 | inspired by the GASP working group at [CCRMA](ccrma.stanford.edu). 52 | 53 | ## License 54 | 55 | The code in this repository is licensed under the GPLv3. 56 | The audio samples in the `Snares/` folder are licensed under 57 | the Creative Commons License. For more information, see 58 | [`Snares/_readme_and_license.txt`](Snares/_readme_and_license.txt). 59 | -------------------------------------------------------------------------------- /Screenshots/Edit.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Screenshots/Edit.gif -------------------------------------------------------------------------------- /Screenshots/Filters.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Screenshots/Filters.gif -------------------------------------------------------------------------------- /Screenshots/Listen.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Screenshots/Listen.gif -------------------------------------------------------------------------------- /Screenshots/PlotBell.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Screenshots/PlotBell.gif -------------------------------------------------------------------------------- /Simulations/DrumFixerNB.py: -------------------------------------------------------------------------------- 1 | # %% 2 | import numpy as np 3 | import scipy.signal as signal 4 | from scipy.io import wavfile 5 | import audio_dspy as adsp 6 | 7 | import matplotlib.pyplot as plt 8 | from matplotlib import ticker 9 | plt.style.use('dark_background') 10 | 11 | # %% [markdown] 12 | # # Modal Signal Processing and Drum Fixing 13 | # 14 | # In this article, we will briefly discuss modal signal 15 | # processing, and show how we can use ideas from the modal 16 | # approach to "fix" drum sounds. 17 | # 18 | # ## Modal Signal Processing 19 | # 20 | # In signal processing, it is often useful to describe a system 21 | # by looking at the "modes" or resonant frequencies of the system. 22 | # Modes are well known in the field of room acoustics, where 23 | # any set of parallel walls will create a mode at the frequency 24 | # corresponding to the distance between the walls. However, many 25 | # systems can be described by their modal characteristics, 26 | # including 27 | # [bells](http://dafx2019.bcu.ac.uk/papers/DAFx2019_paper_34.pdf), 28 | # [cymbals](http://dafx2019.bcu.ac.uk/papers/DAFx2019_paper_48.pdf), 29 | # even [rocket nozzles](https://keirancantilina.github.io/project/rocket-bells/). 30 | # 31 | # While there is a lot of physics that describes why certain objects 32 | # have their specific modal characteristics, in signal processing 33 | # we can often get by with simply measuring the modes and 34 | # resynthesizing them. In order to do this, we need to find three 35 | # parameters for each mode. 36 | # - Mode frequency 37 | # - Mode amplitude (and phase if you like) 38 | # - Mode decay rate 39 | # 40 | # 41 | # For example, if I have a bell with a fundamental frequency 42 | # of 200 Hz, the first mode frequency will most likely be at 43 | # 200 Hz. The amplitude of the mode is essentially the volume 44 | # at which the first mode rings relative to how hard it is hit. 45 | # The decay rate describes how long the bell rings out at that 46 | # frequency. In bells, the higher frequency modes decay very 47 | # quickly, while the lower modes ring out for a long time. 48 | # 49 | # ## Drum Ringing 50 | # 51 | # Now how does modal signal processing relate to drums? Drums can 52 | # be synthesized using modal synthesis, but what about using modal 53 | # ideas to improve the timbre of an existing drum recording? 54 | # Let's examine a snare drum recording taken from 55 | # [freesound.com](https://freesound.com). 56 | 57 | # %% 58 | def plot_specgram(x, fs, fLow=100, title=''): 59 | plt.specgram(x, NFFT=1024, noverlap=512, Fs=fs, cmap='inferno', scale='dB', vmin=-130) 60 | plt.yscale('log') 61 | plt.ylim(fLow, 20000) 62 | plt.gca().yaxis.set_major_formatter(ticker.ScalarFormatter()) 63 | plt.title(title) 64 | plt.xlabel('Time [seconds]') 65 | plt.ylabel('Frequency [Hz]') 66 | 67 | fs, x = wavfile.read('../Snares/hi-snare-1.wav') 68 | x = x[:,0] / 2**15 69 | plot_specgram(x, fs, 200, 'Snare Drum Spectrogram') 70 | 71 | # %% [markdown] 72 | # Note that the snare drum seems to ring out for a very long time 73 | # somewhere around 1200 Hz. Listening to the audio file, confirms 74 | # this: the snare has a nasty high "ring" that I don't find 75 | # particularly pleasing. If I was a mixing engineer tasked with 76 | # mixing this snare drum into a song, I would probably try 77 | # using an EQ to make the snare sound quieter around these 78 | # problematic frequencies. 79 | 80 | # %% 81 | def static_filt(freq, bandwidth, gainDB, sig): 82 | sig_filt = np.copy(sig) 83 | b,a = adsp.design_bell(freq, freq/bandwidth, 10**(gainDB/20), fs) 84 | sig_filt = signal.lfilter(b, a, sig_filt) 85 | return sig_filt 86 | 87 | yEQ_30 = static_filt(1200, 200, -30, x) 88 | yEQ_60 = static_filt(1200, 200, -60, x) 89 | 90 | plt.figure() 91 | plot_specgram(yEQ_30, fs, 200, 'Snare with -30dB EQ') 92 | 93 | plt.figure() 94 | plot_specgram(yEQ_60, fs, 200, 'Snare with -60dB EQ') 95 | 96 | # %% [markdown] 97 | # As you can see it takes a pretty drastic EQ to get the 1200 Hz 98 | # mode to decay in a reasonable amount of time (relative to the 99 | # rest of the drum sound), and using this much EQ kind of ruins 100 | # the attack of the drum sound since it leaves a hole in the 101 | # frequency spectrum. 102 | # 103 | # ## Modal Processing for Drums 104 | # 105 | # But what if we look at this drum ringing problem through the 106 | # lens of modal signal processing? Essentially, the problem 107 | # is that the drum has a mode at 1200 Hz with too long of a 108 | # decay rate. Unfortunately standard EQ can only change the 109 | # amplitude of this mode, not the decay rate. 110 | # 111 | # A couple months ago, my colleague Mark came up with a brilliantly 112 | # simple solution to this problem. Why not use modal analysis to 113 | # determine the decay rate of the ringing mode? Then you could 114 | # implement an EQ filter with a dynamically changing gain that 115 | # could adjust the decay rate of the mode to achieve some desired 116 | # decay time. In other words, when the drum is struck, the filter 117 | # won't affect the signal at all, but as the signal progresses, 118 | # the filter will gradually damp more of the signal so as to 119 | # supress the ringing. 120 | 121 | # %% 122 | from IPython.display import Image 123 | Image(filename='../Screenshots/PlotBell.gif') 124 | 125 | # %% [markdown] 126 | # Let's see how it works! First let's find the decay time of our 127 | # problem mode. We can do this by filtering our signal around the 128 | # mode frequency, measuring the envelope of the signal, and 129 | # finding the slope of the decay. A convenient way to measure 130 | # decay time is often using T60, or the time it takes for the 131 | # signal to decay 60 Decibels. 132 | 133 | # %% 134 | import scipy.stats as stats 135 | def find_decay_time(freq, x, fs, filt_width, thresh=-60, eta=0.005, dBTime=-60): 136 | x_filt = adsp.filt_mode(x, freq, fs, filt_width) 137 | env = adsp.normalize(adsp.energy_envelope(x_filt, fs, eta)) 138 | 139 | plt.plot(x_filt) 140 | plt.plot(env) 141 | plt.title('Energy Envelope of Filtered Signal') 142 | plt.xlabel('Time [samples]') 143 | 144 | start = int(np.argwhere(20 * np.log10(env) > -1)[0]) 145 | if len(np.argwhere(20 * np.log10(env[start:]) < thresh)) == 0: 146 | return 0 147 | end = int(np.argwhere(20 * np.log10(env[start:]) < thresh)[0]) 148 | slope, _, R, _, _ = stats.linregress( 149 | np.arange(len(env[start:end])), 20 * np.log10(env[start:end])) 150 | if R**2 < 0.9: 151 | return 0 152 | 153 | plt.figure() 154 | plt.plot(20*np.log10(np.abs(x_filt))) 155 | plt.plot(20*np.log10(np.abs(env))) 156 | plt.plot(np.arange(len(env))*slope, color='red', linewidth=3) 157 | plt.ylim(-60) 158 | plt.title('Linear Fit of Envelope Slope') 159 | plt.xlabel('Time [Samples]') 160 | plt.ylabel('Magnitude [dB]') 161 | 162 | return (dBTime / slope) / fs 163 | 164 | print('Decay time: ' + str(find_decay_time(1200, x, fs, 200, thresh=-30, dBTime=-45)) + ' seconds') 165 | 166 | # %% [markdown] 167 | # So now that we've determined the T60 of the problematic mode 168 | # to be ~1.5 seconds. Let's pick a desired decay time, say 0.5 169 | # seconds. Now as discussed above, let's say we want to apply 170 | # a time-varying filter to the signal that can create this change 171 | # in T60. What should the the trajectory of the filter gain look 172 | # like? I won't go through all the math here, but essentially, 173 | # you find a gain envelope that corresponds to each T60 time, 174 | # and divide them. When we apply the time-varying filter to our 175 | # signal, here is the result: 176 | 177 | # %% 178 | g_orig = adsp.delay_feedback_gain_for_t60(1, fs, 1.5) 179 | g_des = adsp.delay_feedback_gain_for_t60(1, fs, 0.5) 180 | g_filt = g_des / g_orig 181 | 182 | def apply_filt (x, fs, freq, Q, gain): 183 | filt = adsp.Filter(2, fs) 184 | y = np.zeros_like(x) 185 | 186 | b, a = adsp.design_bell(freq, Q, 10**(-60/20), fs) 187 | filt.set_coefs(b, a) 188 | 189 | for n in range(len(x)): 190 | curGain = gain**n 191 | y[n] = filt.process_sample(x[n]) * (1.0 - curGain) + x[n] * curGain 192 | return y 193 | 194 | y = apply_filt(x, fs, 1200, 1200 / 100, g_filt) 195 | 196 | plt.figure() 197 | plot_specgram(x, fs, 200, 'Dry Signal') 198 | 199 | plt.figure() 200 | plot_specgram(y, fs, 200, 'Processed Signal') 201 | 202 | # %% [markdown] 203 | # From looking at these spectrograms we can see that the ringing 204 | # frequencies have been damped pretty succesfully, without 205 | # affecting the attack of the drum too much. From fine-tuning 206 | # the filter frequency, bandwidth, and desired decay time, it is 207 | # possible to obtain an even better result. 208 | # 209 | # ## Implementation 210 | # 211 | # To make this tool available to mixing engineers and producers 212 | # I've built this algorithm into an audio plugin (VST/AU), 213 | # using the JUCE/C++ framework. To see the source code and 214 | # try it for yourself, check out our 215 | # [GitHub page](https://github.com/jatinchowdhury18/DrumFixer). 216 | # 217 | # Thanks for reading! 218 | 219 | 220 | # %% 221 | -------------------------------------------------------------------------------- /Simulations/snare_sim.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.io import wavfile 3 | import scipy.signal as signal 4 | import matplotlib.pyplot as plt 5 | import audio_dspy as adsp 6 | import scipy.stats as stats 7 | 8 | def find_decay_time(freq, x, fs, filt_width, thresh=-60, eta=0.01, dBTime=-60): 9 | x_filt = adsp.filt_mode(x, freq, fs, filt_width) 10 | env = adsp.normalize(adsp.energy_envelope(x_filt, fs, eta)) 11 | 12 | start = int(np.argwhere(20 * np.log10(env) > -1)[0]) 13 | if len(np.argwhere(20 * np.log10(env[start:]) < thresh)) == 0: 14 | return 0 15 | end = int(np.argwhere(20 * np.log10(env[start:]) < thresh)[0]) 16 | slope, _, R, _, _ = stats.linregress( 17 | np.arange(len(env[start:end])), 20 * np.log10(env[start:end])) 18 | if R**2 < 0.9: 19 | return 0 20 | 21 | return (dBTime / slope) / fs 22 | 23 | def find_decay_times(freqs, x, fs, filt_width, thresh=-60, eta=0.01, dBTime=-60): 24 | times = [find_decay_time(freq, x, fs, filt_width, thresh, eta, dBTime) for freq in freqs] 25 | return times 26 | 27 | fs, x = wavfile.read ('Snares/hi-snare-1.wav') 28 | x = x[:,0] / 2**15 29 | 30 | BANDWIDTH=200 31 | freqs = np.arange(BANDWIDTH/2, 22000, BANDWIDTH) 32 | 33 | # Analyze original signal 34 | times = find_decay_times(freqs, x, fs, BANDWIDTH, thresh=-30, dBTime=-45) 35 | 36 | time_peaks, _ = signal.find_peaks(times) 37 | freq_widths,_,_,_ = signal.peak_widths(times, time_peaks) 38 | print(freqs[time_peaks]) 39 | print(BANDWIDTH * freq_widths) 40 | 41 | plt.figure() 42 | f, t, Sxx = signal.spectrogram (x, fs) 43 | plt.pcolormesh(t, f, 20*np.log10(np.abs(adsp.normalize(Sxx))), cmap='inferno', vmin=-180) 44 | # plt.plot(times, freqs, 'r') 45 | plt.xlim(0, 1.6) 46 | 47 | # Filter spec 48 | filter_freq = freqs[time_peaks[0]] 49 | filterQ = filter_freq / (BANDWIDTH * freq_widths[0]) 50 | print(filterQ) 51 | 52 | original_decay_time = find_decay_time(filter_freq, x, fs, 53 | BANDWIDTH * freq_widths[0], thresh=-30) 54 | desired_decay_time = 0.3 55 | 56 | g_orig = adsp.delay_feedback_gain_for_t60(1, fs, original_decay_time) 57 | g_des = adsp.delay_feedback_gain_for_t60(1, fs, desired_decay_time) 58 | g_filt = g_des / g_orig 59 | 60 | def apply_filt (x, fs, freq, Q, gain): 61 | filt = adsp.Filter(2, fs) 62 | y = np.zeros_like(x) 63 | 64 | b, a = adsp.design_bell(freq, Q, 10**(-60/20), fs) 65 | filt.set_coefs(b, a) 66 | 67 | for n in range(len(x)): 68 | curGain = gain**n 69 | y[n] = filt.process_sample(x[n]) * (1.0 - curGain) + x[n] * curGain 70 | return y 71 | 72 | y = apply_filt(x, fs, filter_freq, filterQ, g_filt) 73 | 74 | plt.figure() 75 | f, t, Syy = signal.spectrogram (y, fs) 76 | plt.pcolormesh(t, f, 20*np.log10(np.abs(adsp.normalize(Syy))), cmap='inferno', vmin=-180) 77 | plt.xlim(0, 1.6) 78 | plt.show() 79 | 80 | wavfile.write('drum_out.wav', fs, y) 81 | -------------------------------------------------------------------------------- /Snares/_readme_and_license.txt: -------------------------------------------------------------------------------- 1 | Sound pack downloaded from Freesound 2 | ---------------------------------------- 3 | 4 | This pack of sounds contains sounds by the following user: 5 | - -zin- ( https://freesound.org/people/-zin-/ ) 6 | 7 | You can find this pack online at: https://freesound.org/people/-zin-/packs/2198/ 8 | 9 | License details 10 | --------------- 11 | 12 | Attribution: http://creativecommons.org/licenses/by/3.0/ 13 | 14 | 15 | Sounds in this pack 16 | ------------------- 17 | 18 | * 34834___zin___Rim_shot_low_snap_1.wav 19 | * url: https://freesound.org/s/34834/ 20 | * license: Attribution 21 | * 34833___zin___Rim_shot_low_no_snap_1.wav 22 | * url: https://freesound.org/s/34833/ 23 | * license: Attribution 24 | * 34832___zin___Rim_shot_hi_snap_1.wav 25 | * url: https://freesound.org/s/34832/ 26 | * license: Attribution 27 | * 34831___zin___Rim_shot_hi_no_snap_1.wav 28 | * url: https://freesound.org/s/34831/ 29 | * license: Attribution 30 | * 34830___zin___Low_snare_2.wav 31 | * url: https://freesound.org/s/34830/ 32 | * license: Attribution 33 | * 34829___zin___Low_snare_1.wav 34 | * url: https://freesound.org/s/34829/ 35 | * license: Attribution 36 | * 34828___zin___Hi_snare_1.wav 37 | * url: https://freesound.org/s/34828/ 38 | * license: Attribution 39 | 40 | 41 | -------------------------------------------------------------------------------- /Snares/hi-snare-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/hi-snare-1.wav -------------------------------------------------------------------------------- /Snares/low-snare-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/low-snare-1.wav -------------------------------------------------------------------------------- /Snares/low-snare-2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/low-snare-2.wav -------------------------------------------------------------------------------- /Snares/rim-shot-hi-no-snap-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/rim-shot-hi-no-snap-1.wav -------------------------------------------------------------------------------- /Snares/rim-shot-hi-snap-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/rim-shot-hi-snap-1.wav -------------------------------------------------------------------------------- /Snares/rim-shot-low-no-snap-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/rim-shot-low-no-snap-1.wav -------------------------------------------------------------------------------- /Snares/rim-shot-low-snap-1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinchowdhury18/DrumFixer/487bab3c480326204b60ff0690bb6eeed1c262f0/Snares/rim-shot-low-snap-1.wav --------------------------------------------------------------------------------