Comments (2)
Hy again,
yesterday I found a solution for my issue, but i dont know if its the best way to do this...
The methods GetWAVEData(), WaveHeaderIN() & WaveHeaderOUT() is from codeproject to get Wave datas without the wave header and write it back into the source wave file.
[http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C]
This Methode is also from the codeproject sourcecode:
public bool ChangeVolume(string strPath, bool booIncrease, short shtPcnt)
{
if (strPath == null) strPath = "";
if (strPath == "") return false;
if (shtPcnt > 100) return false;
CustomWaveProcessor wain = new CustomWaveProcessor();
CustomWaveProcessor waout = new CustomWaveProcessor();
waout.DataLength = waout.Length = 0;
if (!wain.WaveHeaderIN(@strPath)) return false;
waout.DataLength = wain.DataLength;
waout.Length = wain.Length;
waout.BitsPerSample = wain.BitsPerSample;
waout.Channels = wain.Channels;
waout.SampleRate = wain.SampleRate;
byte[] arrfile = this.GetWAVEData(strPath);
//change volume
for (int j = 0; j < arrfile.Length; j += 2)
{
short snd = this.ComplementToSigned(ref arrfile, j);
try
{
short p = Convert.ToInt16((snd * shtPcnt) / 100);
if (booIncrease)
snd += p;
else
snd -= p;
}
catch
{
snd = this.ComplementToSigned(ref arrfile, j);
}
byte[] newval = this.SignedToComplement(snd);
if ((newval[0] != null) && (newval[1] != null))
{
arrfile[j] = newval[0];
arrfile[j + 1] = newval[1];
}
}
//write back to the file
waout.DataLength = arrfile.Length;
waout.WaveHeaderOUT(@strPath);
this.WriteWAVEData(strPath, ref arrfile);
return true;
}
With this Method i am able to get the current amplitude value of my wave file:
public double GetAmplitudeFromWave(string strPath)
{
byte[] _buffer = this.GetWAVEData(strPath);
double peak = 0;
for (var i = 0; i < _buffer.Length; i = i + 2)
{
var sample = BitConverter.ToInt16(_buffer, i);
if (sample > peak)
peak = sample;
else if (sample < -peak)
peak = -sample;
}
var decibel = (20 * Math.Log10(peak / 32768));
return decibel;
}
Finally i wrote this Method to set my custom amplitude value for my wave file:
public void ChangeAmplitude(string strPath, double outDb)
{
try
{
double amp = this.GetAmplitudeFromWave(strPath);
Console.WriteLine("Current dB:" + amp);
if (amp > outDb)
{
while (amp > outDb)
{
this.ChangeVolume(strPath, false, 1);
amp = this.GetAmplitudeFromWave(strPath);
}
}
else if (amp < outDb)
{
while (amp < outDb)
{
this.ChangeVolume(strPath, true, 1);
amp = this.GetAmplitudeFromWave(strPath);
}
}
Console.WriteLine("dB changed to: " + amp);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
What are you saying, is it OK to do it so or is there any other solution?
Thx,
Greetz
from naudio.
Really overcomplicated, I´d simply use a VolumeSampleProvider or would code a custom ISampleProvider.
from naudio.
Related Issues (20)
- MediaFoundationReader doesnt work with opus format HOT 1
- naudio.core is missing NuGet package README file
- I am trying to make i program to play live microphone audio over the live audio out channel but i am having issues
- Initializing a Wasapi device can tak a long time
- Audio Playback Speed Issue with Multiple Buses in WaveMixerStream32
- why the wavePlayer.PlaybackState was changed to PlaybackState.stopped when I call wavePlayer.Pause()
- Not a standard WAVE PCM soundfile format
- Unable to set SetDuckingPreference HOT 1
- Windows-Independent Mp3FileReader, Load MP3 with NAudio.Core
- Set Licence Expression on package info
- Make Windows.Win32.Registry a conditional package based on TFM
- MediaFoundationReader is not working after I deployed the project to azure HOT 1
- OGG file format support HOT 3
- Bug: possible memory leak
- WasapiLoopbackCapture Http Live Stream HOT 2
- IAudioSessionEventsHandler.OnStateChanged not firing when used inside windows service c# HOT 3
- .Net Maui IOS crashing HOT 1
- System.MemoryAccessViolation Exception for WaveInEvent
- Read method of Pcm24BitToSampleProvider ignores the offset parameter HOT 3
- waveout ini display error ,can't find a supporte foramt HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from naudio.