C
C#2mo ago
HiveMind

Add sound to audio input

Hi! I have an api that takes an audio from some input, e.g. microphone, and passes it furher to some external device. How can I add some sounds, or modify the sound before its passed to audio port? I need to add some distortion to the audio, like e.g. phone talk with very pool connection. Unfortunately, I can't modify the api
6 Replies
Sossenbinder
Sossenbinder2mo ago
Sounds like you'd want to use a library like NAudio and modify the recording ahead of sending it to the API
HiveMind
HiveMind2mo ago
The api takes data automatically. I guess I need to just modify the sound that is passed to audio output port. I guess I can do it somehow in NAudio, but some hits would be really useful
Sossenbinder
Sossenbinder2mo ago
So, I ain't a big audio guy, but I just tried around a bit with (in full transparency) AI-generated code
using (var reader = new AudioFileReader(inputFile))
using (var writer = new WaveFileWriter(outputFile, reader.WaveFormat))
{

float[] buffer = new float[1024];
int samplesRead;

float cutoffFrequency = 2000f;
float dropoutChance = 0.05f;
float bitReductionFactor = 0.2f;

float previousOutput = 0f;

while ((samplesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
for (int i = 0; i < samplesRead; i++)
{
if (Random.Shared.NextDouble() < dropoutChance)
{
buffer[i] = 0;
}
else
{
buffer[i] = (float)(Math.Round(buffer[i] / bitReductionFactor) * bitReductionFactor);

float currentSample = buffer[i];
buffer[i] = previousOutput + (cutoffFrequency / reader.WaveFormat.SampleRate) * (currentSample - previousOutput);
previousOutput = buffer[i];
}

buffer[i] *= (float)(0.9 + Random.Shared.NextDouble() * 0.2);
}

writer.WriteSamples(buffer, 0, samplesRead);
}
}
using (var reader = new AudioFileReader(inputFile))
using (var writer = new WaveFileWriter(outputFile, reader.WaveFormat))
{

float[] buffer = new float[1024];
int samplesRead;

float cutoffFrequency = 2000f;
float dropoutChance = 0.05f;
float bitReductionFactor = 0.2f;

float previousOutput = 0f;

while ((samplesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
for (int i = 0; i < samplesRead; i++)
{
if (Random.Shared.NextDouble() < dropoutChance)
{
buffer[i] = 0;
}
else
{
buffer[i] = (float)(Math.Round(buffer[i] / bitReductionFactor) * bitReductionFactor);

float currentSample = buffer[i];
buffer[i] = previousOutput + (cutoffFrequency / reader.WaveFormat.SampleRate) * (currentSample - previousOutput);
previousOutput = buffer[i];
}

buffer[i] *= (float)(0.9 + Random.Shared.NextDouble() * 0.2);
}

writer.WriteSamples(buffer, 0, samplesRead);
}
}
Sossenbinder
Sossenbinder2mo ago
This is the result So it seems like you can definitely get somewhere pretty quickly
Want results from more Discord servers?
Add your server