From 70969ea2a9c996021ec063514c478c19b1243758 Mon Sep 17 00:00:00 2001 From: Florent Berthaut <florent.berthaut@univ-lille.fr> Date: Mon, 3 Apr 2023 11:51:38 +0200 Subject: [PATCH] Added non pitched additive synthesis --- src/modules/ProjectorModule.cpp | 5 ++--- src/shaders/render43.fs | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/modules/ProjectorModule.cpp b/src/modules/ProjectorModule.cpp index f3133f6..9eb8a59 100644 --- a/src/modules/ProjectorModule.cpp +++ b/src/modules/ProjectorModule.cpp @@ -191,9 +191,8 @@ ProjectorModule::ProjectorModule(): Module() { //audio - m_nbTracks=50; - //m_maxAudioValue = (std::numeric_limits<int>::max())/(m_width * m_height); - m_maxAudioValue = (std::numeric_limits<int>::max())/(m_width); + m_nbTracks=3000; + m_maxAudioValue = (std::numeric_limits<int>::max())/(m_width * m_height); m_audioMixSize = AudioManager::getInstance()->getMixSize(); m_audioBufSize = AudioManager::getInstance()->getBufferSize(); m_imgBufSize = m_audioBufSize+3; diff --git a/src/shaders/render43.fs b/src/shaders/render43.fs index e7d5d17..32f3496 100644 --- a/src/shaders/render43.fs +++ b/src/shaders/render43.fs @@ -110,6 +110,46 @@ float random (vec3 st){ out vec4 color; void additiveSynth(vec4 color) { + + int nbTracks=3000; + float note = 20.0 + floor(color.x * float(nbTracks))/float(nbTracks)*80.0; + int ind = int(color.x*float(nbTracks)); + float freq=0.0; + + //count this fragment + if(imageAtomicAdd(audioTex, ivec2(0, ind), 1)<1) { + //if the audio has not been generated by another fragment + //compute frequency + freq = float(440.0 * pow(2.0, (note-69.0)/12.0)); + //retrieve either new or old phase, depending on if audio was processed + float phase = float(imageLoad(audioTex, ivec2(1, ind)).x); + if(audioNextBuf>0) {//if new phase + //retrieve it + phase = float(imageLoad(audioTex, ivec2(2, ind)).x); + //store it as previous phase + imageStore(audioTex, ivec2(1, ind), ivec4(int(phase))); + } + + float outputPhase = phase; + float samplesPerPeriod = (audioRate/freq); + float phaseStep = 1.0; + for(int i=0; i<audioBufSize; i++) { + //compute and store value starting from 3rd column + float s = sin((phase/samplesPerPeriod) * 2.0 * M_PI); + imageStore(audioTex, ivec2(i+3, ind), ivec4(int(s*maxAudioValue))); + + //increase phase + phase=mod(phase + phaseStep, samplesPerPeriod); + } + + //remove audioMixSize steps and write phase to output + outputPhase=mod(outputPhase+phaseStep*(audioBufSize-audioMixSize), + samplesPerPeriod); + imageStore(audioTex, ivec2(2, ind), ivec4(int(outputPhase))); + } +} + +void pitchedAdditiveSynth(vec4 color) { float freq=0.0; float note = 20 + color.x * 80; int ind=0; -- GitLab