Need help with Naudio.dll Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
keylo
Posts: 52
Joined: 21 Oct 2020, 21:03

Need help with Naudio.dll

22 Jul 2021, 22:41

Hi all,I found a Dll library called NAUDIO.dll, useful to manipulate wav audio files. of course, I could easily use "Sox Exchange" but I prefer to use a library with a more permissive licence and since naudio use the MIT licence, I wanna give it a try

Mainly, I want to use it to trim (split) wav files and add silences to a wav file. I found that does trimming in C . can this code be translated to AutoHotkey using DllCall ?
a hint or anything to get me started would be much appreciated. Also please let me know about any other options I might overlook.

The naudio.dll can be found here : https://www.softpedia.com/dyn-postdownload.php/e6dbe2dc9166a52d65dd4a21e5ca0542/60fa36f2/1a30f/0/1

https://markheath.net/post/trimming-wav-file-using-naudio

Code: Select all

public static class WavFileUtils
{
    public static void TrimWavFile(string inPath, string outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
    {
        using (WaveFileReader reader = new WaveFileReader(inPath))
        {
            using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat))
            {
                int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;

                int startPos = (int)cutFromStart.TotalMilliseconds * bytesPerMillisecond;
                startPos = startPos - startPos % reader.WaveFormat.BlockAlign;

                int endBytes = (int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;
                endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
                int endPos = (int)reader.Length - endBytes; 

                TrimWavFile(reader, writer, startPos, endPos);
            }
        }
    }

    private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos)
    {
        reader.Position = startPos;
        byte[] buffer = new byte[1024];
        while (reader.Position < endpos)
        {
            int bytesRequired = (int)(endPos - reader.Position);
            if (bytesRequired > 0)
            {
                int bytesToRead = Math.Min(bytesRequired, buffer.Length);
                int bytesRead = reader.Read(buffer, 0, bytesToRead);
                if (bytesRead > 0)
                {
                    writer.WriteData(buffer, 0, bytesRead);
                }
            }
        }
    }
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Need help with Naudio.dll

23 Jul 2021, 03:59

keylo wrote:
22 Jul 2021, 22:41
does trimming in C
this is C#, not C
u could probably load the dll using https://www.autohotkey.com/boards/viewtopic.php?t=4633 and take it from there
can this code be translated to AutoHotkey using DllCall ?
depends what ure asking
can u use DllCall to outright call these functions? no.
can this code be translated to AutoHotkey...? yes, wave hacking is just bit manipulations. learn the WAV format and u can reimplement it using the FileObject for exmaple
... using DllCall ? yes, u could bypass plain ahk and use DllCalls instead. not sure what the point of doing that would be, but u could do it nonetheless
keylo
Posts: 52
Joined: 21 Oct 2020, 21:03

Re: Need help with Naudio.dll

23 Jul 2021, 16:06

Thank you @swagfag for your guidance, so if I understand, I can do all the wave hacking (trim, volume, etc..) just using AutoHotkey FileObject ?
no need for any external library? That's even better.


I have started to learn the wave format (FileTypeBlocID, BlocSize, etc...) I also found jeeswg's File object mini-tutorial https://www.autohotkey.com/boards/viewtopic.php?f=74&t=66674, very well done.
I'm all excited about this project now.

At the same time I'm a bit worry, why nobody has done wav manipulation using AHK yet. is it more complicated than it looks?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Need help with Naudio.dll

23 Jul 2021, 16:47

keylo wrote:
23 Jul 2021, 16:06
I can do all the wave hacking (trim, volume, etc..) just using AutoHotkey FileObject ?
i dont know, im not familiar with audio. whatever bit manipulations the format permits, u can do with the FileObj
At the same time I'm a bit worry, why nobody has done wav manipulation using AHK yet. is it more complicated than it looks?
if u need to do DSP(which most often eventually ends up being the case), u dont do it in slow, interpreted languages
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Need help with Naudio.dll

23 Jul 2021, 17:12

keylo, You can do it with Microsoft Media Foundation api
https://docs.microsoft.com/en-us/windows/win32/medfound/microsoft-media-foundation-sdk
Or with uwp api (Not 100% sure)
https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/
But it will be rather complex code, and it is easier to use ready-made libraries for such tasks.
keylo
Posts: 52
Joined: 21 Oct 2020, 21:03

Re: Need help with Naudio.dll solved :)  Topic is solved

24 Jul 2021, 01:50

@malcev Thank you, while Media Foundation seems very powerful, the learning curve seems to be too steep for my skill at the moment.
Of course, bringing the Media Foundation to Autohotkey would be a dream come true
but what I was trying to do is a very basic audio manipulation like muting a part of an audio file.

I followed @swagfag guidance I learned the wav file format (http://soundfile.sapp.org/doc/WaveFormat/)
and came up with something really good using AHK's file object.

It seems to be as fast or maybe faster than other external libraries I was using to do this.
This is a rough code, I'm pretty sure it can be improved but this is the best I can do at the moment
Please feel free to provide me with any tips to improve it.

Code: Select all

; created by keylo
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines, -1

;~ SilenceWav_From_To(WavFile,From,to)
SilenceWav_From_To("daft_punk_lucky.WAV",3.5,5.5)
ExitApp


;~ **********************************************
;~ **********************************************
;~ From, to :  position in second
SilenceWav_From_To(WavFile,From,to)
{
Start := Get_BytePosition(WavFile,From)
end := Get_BytePosition(WavFile,to)
file := FileOpen(WavFile, "rw")
FileLength := File.Length - 1
file.Position := Start
		loop,
		{
		Mypos := file.Position
		if mypos between %Start% and %end%
		file.WriteUInt(00000000)
		 if (mypos >= end) || if (Mypos >= FileLength)
			break
		}
File.Close()
}
;~ ************************************************************
;~ ************************************************************


;~ ************************************************************
;~ **************************************************************
;~ **************************************************************
Get_SampleRate(vpath) ; vpath is the wavefile
{
	file := FileOpen(vPath, "r")
	file.Position := 24
	return file.ReadUInt()  ; 44100  48000 ... 
}
;~ **************************************************************
Get_bitsPerSample(vpath)
{
	file := FileOpen(vPath, "r")
	file.Position := 34
	num := file.ReadUInt()
	RegExMatch(num, "^\d\d", num)
	return num ; 16 24 32 ...
}
;~ **************************************************************
Get_numChannels(vpath)
{
	file := FileOpen(vPath, "r")
	file.Position := 22
	num := file.ReadUInt()
	num := format("{:08x}", num) 
	RegExMatch(num, "\d$", num)
	return num
	;~ 1 - mono
	;~ 2 - Stereo
}
;~ **************************************************************
Get_BytePosition(vpath,Seconds)
{
SamplesPerSecond := Get_SampleRate(vpath)
numChannels := Get_numChannels(vpath)
bitsPerSample := Get_bitsPerSample(vpath)
sampleNumber := Floor(Seconds * SamplesPerSecond * numChannels)
bytePos := sampleNumber * (bitsPerSample/8)
return round(bytePos)
}
;~ **************************************************************
;~ *******************************************
;~ *******************************************
;~ *******************************************

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq and 428 guests