[Library] fmod fsound wrapper

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Spawnova
Posts: 555
Joined: 08 Jul 2015, 00:12
Contact:

[Library] fmod fsound wrapper

12 Jun 2020, 04:58

Hey guys, I needed a sound library for my game engine and I could not find any working links or scripts for any fsound wrappers in AutoHotKey so I wrote my own and I thought I would share it here.

This is for fmod.dll version 3.75
It's not all the functions but it's a good amount of them, I only really needed the sound playback stuff so everything else I added just in case someone would find it useful.

This is also my first time releasing a library so if I've made mistakes or there are better ways to do things please let me know. I also don't know if it will work with ahk 64 bit.

I could not find an official download page the dll, so I've uploaded it to my dropbox and you can download it from here or search google =P


For people who may not know how to start using the library here is an example of 2 ways to play sounds

Code: Select all

#noenv
#persistent
#singleinstance,force
#include fmod_fsound.ahk

;downloads 2 example sounds from my dropbox
IfNotExist,file1.wav
	urldownloadtofile,https://www.dropbox.com/s/yilpqgv1ypwwemx/file1.wav?dl=1,file1.wav
IfNotExist,file2.wav
	urldownloadtofile,https://www.dropbox.com/s/p4l8crryn76rb7d/file2.wav?dl=1,file2.wav

fsound := new fmod_fsound() ;Create new instance of fmod_fsound class

fsound.Init() ;Initialize the Fsound system

sound1 := fsound.StreamOpen("file1.wav") 	;prepare file for streaming
fsound.StreamSetMode(sound1,2) 				;mode 2 = FSOUND_LOOP_NORMAL
fsound.StreamSetLoopCount(sound1,19) 		;loop 19 additional times, 20 total
fsound.StreamPlay(sound1)
sleep 3000
fsound.StreamStop(sound1) 					;after 3 seconds stop the stream

sleep 1000

sound2 := fsound.SampleLoad("file2.wav") 	;load file2.wav into memory

loop 20 { 									;play sound randomly
	fsound.PlaySound(sound2)
	random,sleepTime,10,300
	sleep % sleepTime
}

fsound.StreamClose(sound1) 					;close stream
fsound.SampleFree(sound2) 					;free sample from memory
fsound.Close() 								;shutdown fsound
exitapp

And here is the actual library

Code: Select all

/* 
6/5/2020 simple FMOD.dll FSOUND wrapper by Spawnova
for Fmod.dll version 3.75

For more information on FSOUND visit the documentation here https://documentation.help/fmod/fsound
*/

class fmod_fsound {
	static ERRORS := {0:"FMOD_ERR_NONE",1:"FMOD_ERR_BUSY",2:"FMOD_ERR_UNINITIALIZED",3:"FMOD_ERR_INIT",4:"FMOD_ERR_ALLOCATED"
	,5:"FMOD_ERR_PLAY",6:"FMOD_ERR_OUTPUT_FORMAT",7:"FMOD_ERR_COOPERATIVELEVEL",8:"FMOD_ERR_CREATEBUFFER",9:"FMOD_ERR_FILE_NOTFOUND"
	,10:"FMOD_ERR_FILE_FORMAT",11:"FMOD_ERR_FILE_BAD",12:"FMOD_ERR_MEMORY",13:"FMOD_ERR_VERSION",14:"FMOD_ERR_INVALID_PARAM"
	,15:"FMOD_ERR_NO_EAX",16:"FMOD_ERR_CHANNEL_ALLOC",17:"FMOD_ERR_RECORD",18:"FMOD_ERR_MEDIAPLAYER",19:"FMOD_ERR_CDDEVICE"}
	
	__New(dllPath="FMOD.dll") {
		ifnotexist,%dllPath%
		{
			msgbox % "Error: File Not Found!`n`n" a_tab dllPath
			return 0
		}
		this.lib := DllCall("LoadLibrary", "Str", dllPath,"ptr")
		
		

		;#########################################################################################Function Memory Pointers
		this._3D_GetMinMaxDistance := 	this.GPA("_FSOUND_3D_GetMinMaxDistance@12")
		this._Close := 					this.GPA("_FSOUND_Close@0")
		this._FX_Disable := 			this.GPA("_FSOUND_FX_Disable@4")
		this._FX_Enable := 				this.GPA("_FSOUND_FX_Enable@8")
		this._FX_SetChorus := 			this.GPA("_FSOUND_FX_SetChorus@32")
		this._FX_SetEcho := 			this.GPA("_FSOUND_FX_SetEcho@24")
		this._FX_SetGargle := 			this.GPA("_FSOUND_FX_SetGargle@12")
		this._GetAmplitude := 			this.GPA("_FSOUND_GetAmplitude@4")
		this._GetChannelsPlaying := 	this.GPA("_FSOUND_GetChannelsPlaying@0")
		this._GetCPUUsage := 			this.GPA("_FSOUND_GetCPUUsage@0")
		this._GetCurrentLevels := 		this.GPA("_FSOUND_GetCurrentLevels@12")
		this._GetCurrentPosition := 	this.GPA("_FSOUND_GetCurrentPosition@4")
		this._GetCurrentSample :=		this.GPA("_FSOUND_GetCurrentSample@4")
		this._GetDriver := 				this.GPA("_FSOUND_GetDriver@0")
		this._GetDriverName := 			this.GPA("_FSOUND_GetDriverName@4")
		this._GetFrequency := 			this.GPA("_FSOUND_GetFrequency@4")
		this._GetLastError := 			this.GPA("_FSOUND_GetError@0")
		this._GetLoopMode := 			this.GPA("_FSOUND_GetLoopMode@4")
		this._GetMaxChannels := 		this.GPA("_FSOUND_GetMaxChannels@0")
		this._GetMaxSample := 			this.GPA("_FSOUND_GetMaxSamples@0")
		this._GetMemoryStats := 		this.GPA("_FSOUND_GetMemoryStats@8")
		this._GetMixer := 				this.GPA("_FSOUND_GetMixer@0")
		this._GetMute := 				this.GPA("_FSOUND_GetMute@4")
		this._GetOutputRate := 			this.GPA("_FSOUND_GetOutputRate@0")
		this._GetPan := 				this.GPA("_FSOUND_GetPan@4")
		this._GetPaused := 				this.GPA("_FSOUND_GetPaused@4")
		this._GetPriority := 			this.GPA("_FSOUND_GetPriority@4")
		this._GetReserved := 			this.GPA("_FSOUND_GetReserved@4")
		this._GetSurround := 			this.GPA("_FSOUND_GetSurround@4")
		this._GetVersion := 			this.GPA("_FSOUND_GetVersion@0")
		this._GetVolume := 				this.GPA("_FSOUND_GetVolume@4")
		this._Init := 					this.GPA("_FSOUND_Init@12")
		this._IsPlaying := 				this.GPA("_FSOUND_IsPlaying@4")
		this._PlaySound := 				this.GPA("_FSOUND_PlaySound@8")
		this._PlaySoundEx := 			this.GPA("_FSOUND_PlaySoundEx@16")
		this._RecordGetDriver := 		this.GPA("_FSOUND_Record_GetDriver@0")
		this._SampleFree := 			this.GPA("_FSOUND_Sample_Free@4")
		this._SampleGet := 				this.GPA("_FSOUND_Sample_Get@4")
		this._SampleGetDefaults := 		this.GPA("_FSOUND_Sample_GetDefaults@20")
		this._SampleGetLength := 		this.GPA("_FSOUND_Sample_GetLength@4")
		this._SampleGetMinMaxDistance :=this.GPA("_FSOUND_Sample_GetMinMaxDistance@12")
		this._SampleGetName := 			this.GPA("_FSOUND_Sample_GetName@4")
		this._SampleLoad := 			this.GPA("_FSOUND_Sample_Load@20")
		this._SampleSetDefaults := 		this.GPA("_FSOUND_Sample_SetDefaults@20")
		this._SampleSetMaxPlaybacks := 	this.GPA("_FSOUND_Sample_SetMaxPlaybacks@8")
		this._SampleSetMinMaxDistance :=this.GPA("_FSOUND_Sample_SetMinMaxDistance@12")
		this._SampleSetMode := 			this.GPA("_FSOUND_Stream_SetMode@8")
		this._SetCurrentPosition := 	this.GPA("_FSOUND_SetCurrentPosition@8")
		this._SetFrequency := 			this.GPA("_FSOUND_SetFrequency@8")
		this._SetHWND := 				this.GPA("_FSOUND_SetHWND@4")
		this._SetLoopMode := 			this.GPA("_FSOUND_SetLoopMode@8")
		this._SetMinHardwareChannels := this.GPA("_FSOUND_SetMinHardwareChannels@4")
		this._SetMute := 				this.GPA("_FSOUND_SetMute@8")
		this._SetPan := 				this.GPA("_FSOUND_SetPan@8")
		this._SetPaused := 				this.GPA("_FSOUND_SetPaused@8")
		this._SetPriority := 			this.GPA("_FSOUND_SetPriority@8")
		this._SetReserved := 			this.GPA("_FSOUND_SetReserved@8")
		this._SetSurround := 			this.GPA("_FSOUND_SetSurround@8")
		this._SetVolume := 				this.GPA("_FSOUND_SetVolume@8")
		this._SetVolumeAbsoulute := 	this.GPA("_FSOUND_SetVolumeAbsolute@8")
		this._StopSound := 				this.GPA("_FSOUND_StopSound@4")
		this._StreamClose := 			this.GPA("_FSOUND_Stream_Close@4")
		this._StreamGetLength := 		this.GPA("_FSOUND_Stream_GetLength@4")
		this._StreamGetLengthMs := 		this.GPA("_FSOUND_Stream_GetLengthMs@4")
		this._StreamGetOpenState := 	this.GPA("_FSOUND_Stream_GetOpenState@4")
		this._StreamGetPosition := 		this.GPA("_FSOUND_Stream_GetPosition@4")
		this._StreamGetSample := 		this.GPA("_FSOUND_Stream_GetSample@4")
		this._StreamGetTime := 			this.GPA("_FSOUND_Stream_GetTime@4")
		this._StreamOpen := 			this.GPA("_FSOUND_Stream_Open@16")
		this._StreamPlay := 			this.GPA("_FSOUND_Stream_Play@8")
		this._StreamPlayEx := 			this.GPA("_FSOUND_Stream_PlayEx@16")
		this._StreamSetEndCallback := 	this.GPA("_FSOUND_Stream_SetEndCallback@12")
		this._StreamSetLoopCount := 	this.GPA("_FSOUND_Stream_SetLoopCount@8")
		this._StreamSetMode := 			this.GPA("_FSOUND_Stream_SetMode@8")
		this._StreamSetPosition := 		this.GPA("_FSOUND_Stream_SetPosition@8")
		this._StreamSetTime := 			this.GPA("_FSOUND_Stream_SetTime@8")
		this._StreamStop := 			this.GPA("_FSOUND_Stream_Stop@4")
		this._Update := 				this.GPA("_FSOUND_Update@0")
	}
	
	;https://documentation.help/fmod/FSOUND_3D_GetMinMaxDistance.html
	;Returns the current min and max distance for a channel.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	3D_GetMinMaxDistance(channel,ByRef min, Byref max) {
		return dllcall(this._3D_GetMinMaxDistance, Int,channel, FloatP,min, FloatP,max, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Close.html
	;Shuts down the WHOLE FMOD Sound System.
	Close() {
		dllcall(this._Close)
	}
	
	;https://documentation.help/fmod/FSOUND_FX_Disable.html
	;Disables effect processing for ALL effects on the specified channel.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	FX_Disable(channel,fxType) {
		return dllcall(this._FX_Disable, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_FX_Enable.html
	;Enables effect processing for the specified channel. 
	;This command continues to add effects to a channel (up to 16) until FSOUND_FX_Disable is called.
	;On success, an FX id is returned.
	;On failure, -1 is returned.
	FX_Enable(channel,fxType) {
		return dllcall(this._FX_Enable, Int,channel, UInt,fxType)
	}

	;https://documentation.help/fmod/FSOUND_FX_SetChorus.html
	;Sets the parameters for the chorus effect on a particular channel
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	FX_SetChorus(fxid,WetDryMix=0,Depth=25,Feedback=0,Frequency=0,Waveform=1,Delay=0,Phase=2) {
		return dllcall(this._FX_SetChorus, Int,fxid, Float,WetDryMix, Float,Depth, Float,Feedback, Float,Frequency, Int,Waveform, Float,Delay, Int,Phase, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_FX_SetEcho.html
	;Sets the parameters for the echo effect on a particular channel
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	FX_SetEcho(fxid,WetDryMix=0,Feedback=0,leftDelay=333,rightDelay=333,panDelay=0) {
		return dllcall(this._FX_SetEcho, Int,fxid, Float,WetDryMix, Float,Feedback, Float,leftDelay, Float,rightDelay, Int,panDelay, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_FX_SetGargle.html
	;Sets the parameters for the gargle effect on a particular channel
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	FX_SetGargle(fxid,RateHz,WaveShape) {
		return dllcall(this._FX_SetGargle, Int,fxid, Int,RateHz, Int,WaveShape, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_GetAmplitude.html
	;Returns the volume of the channel based on all combinations of set volume, mastervolume and 3d position.
	;Works on software and hardware voices.
	;On success, the following values are returned : 0 = silent to 255 = full volume.
	;On failure, 0 is returned. To quailfy if this is a real error, call FSOUND_GetError.
	GetAmplitude(channel) {
		return dllcall(this._GetAmplitude, Int,channel)
	}
	
	;https://documentation.help/fmod/FSOUND_GetChannelsPlaying.html
	;Returns the number of active channels in FSOUND, or ones that are playing.
	GetChannelsPlaying() {
		return dllcall(this._GetChannelsPlaying)
	}
	
	;https://documentation.help/fmod/FSOUND_GetCPUUsage.html
	;Returns in percent of cpu time the amount of cpu usage that FSOUND/FMUSIC mixing is taking.
	;floating point value between 0.0 and 100.0.
	GetCPUUsage() {
		return dllcall(this._GetCPUUsage, Float)
	}
	
	;https://documentation.help/fmod/FSOUND_GetCurrentLevels.html
	;Returns a left and right VU/Level reading at the current position of the specified channel.
	;Levels are are only supported for software channels.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	GetCurrentLevels(channel,byref leftChannel,byref rightChannel) {
		return dllcall(this._GetCurrentLevels, Int,channel,	FloatP,leftChannel, FloatP,rightChannel)
	}
	
	;https://documentation.help/fmod/FSOUND_GetCurrentPosition.html
	;Returns the current playcursor position of the specified channel.
	;On success, the play cursor position in SAMPLES is returned for the specified channel.
	;On failure, 0 is returned.
	GetCurrentPosition(channel) {
		return dllcall(this._GetCurrentPosition, Int,channel, UInt)
	}
	
	;https://documentation.help/fmod/FSOUND_GetCurrentSample.html
	;Returns the current sample being played on the specified channel.
	;On success, a pointer to a sample handle is returned for the specified channel.
	;On failure, NULL is returned.
	GetCurrentSample(channel) {
		return dllcall(this._GetCurrentSample, Int,channel, Ptr)
	}
	
	;https://documentation.help/fmod/FSOUND_GetDriver.html
	;Returns the currently selected driver number. Drivers are enumerated when selecting a driver
	;with FSOUND_SetDriver or other driver related functions such as FSOUND_GetNumDrivers or FSOUND_GetDriverName
	GetDriver() {
		return dllcall(this._GetDriver)
	}
	
	;https://documentation.help/fmod/FSOUND_GetDriverName.html
	;Returns the name of the selected driver. Drivers are enumerated when selecting a driver with
	;FSOUND_SetDriver or other driver related functions such as FSOUND_GetNumDrivers or FSOUND_GetDriver
	GetDriverName(id=0) {
		strPointer := dllcall(this._GetDriverName, Int,id, Ptr)
		if (strPointer)
			return strget(strPointer,,"UTF-8")
		return 0
	}
	
	;https://documentation.help/fmod/FSOUND_GetFrequency.html
	;Returns the frequency in HZ of the specified channel.
	;On success, the frequency in HZ of the specified channel is returned.
	;On failure, 0 is returned. To quailfy if this is a real error, call GetLastError().
	GetFrequency(channel) {
		return dllcall(this._GetFrequency, Int,channel)
	}
	
	;https://documentation.help/fmod/FSOUND_GetError.html
	;Returns error string enum from last error
	GetLastError() {
		err := dllcall(this._GetLastError)
		return err " = " this.ERRORS[err]
	}
	
	;https://documentation.help/fmod/FSOUND_GetLoopMode.html
	;Gets the loop mode for a particular channel.
	;On success, the loop mode is returned.
	;On failure, 0 is returned.	
	GetLoopMode(channel) {
		return dllcall(this._GetLoopMode, Int,channel, UInt)
	}
	
	;https://documentation.help/fmod/FSOUND_GetMaxChannels.html
	;Returns the total number of channels allocated.
	GetMaxChannels() {
		return dllcall(this._GetMaxChannels)
	}
	
	;https://documentation.help/fmod/FSOUND_GetMaxSamples.html
	;Returns the current maximum index for a sample. This figure grows as you allocate more
	;samples (in blocks)
	GetMaxSample() {
		return dllcall(this._GetMaxSample)
	}
	
	;https://documentation.help/fmod/FSOUND_GetMemoryStats.html
	;Returns information on the memory usage of fmod. This is useful for determining a fixed memory size to
	;make FMOD work within for fixed memory machines such as pocketpc and consoles.
	GetMemoryStats(ByRef currentAlloc,Byref maxAlloc) {
		dllcall(this._GetMemoryStats, UIntP,currentAlloc, UIntP,maxAlloc)
	}
	
	
	;https://documentation.help/fmod/FSOUND_GetMixer.html
	;Returns the currently used mixer type.
	GetMixer() {
		return dllcall(this._GetMixer)
	}
	
	;https://documentation.help/fmod/FSOUND_GetMute.html
	;Returns if the channel specified is muted or not
	;TRUE - The channel has mute turned ON
	;FALSE - The channel has mute turned OFF
	GetMute(channel) {
		return dllcall(this._GetMute, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_GetOutputRate.html
	;Returns the current mixing rate in HZ
	GetOutputRate() {
		return dllcall(this._GetOutputRate)
	}
	
	;https://documentation.help/fmod/FSOUND_GetPan.html
	;Returns the linear pan position of the specified channel between 0 and 255
	;On success, the following values are returned : 0 = full left to 128 = middle to 255 = full right, FSOUND_STEREOPAN
	;On failure, 0 is returned. To quailfy if this is a real error, call GetLastError()
	GetPan(channel) {
		return dllcall(this._GetPan, Int,channel)
	}
	
	;https://documentation.help/fmod/FSOUND_GetPaused.html
	;Gets current pause status of the channel.
	;TRUE - The channel is currently paused.
	;FALSE - The channel is running.
	GetPaused(channel) {
		return dllcall(this._GetPaused, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_GetPriority.html
	;Gets a sound channels priority. Priority is used to determine if soundeffects should
	;replace other sound effects when the channel limit has been reached.
	;On success, the priority of the channel is returned. Ranges between 0 and 255.
	;On failure, 0 is returned. To quailfy if this is a real error, call GetLastError().	
	GetPriority(channel) {
		return dllcall(this._GetPriority, Int, channel)
	}
	
	;https://documentation.help/fmod/FSOUND_GetReserved.html
	;Gets a sound channels reserved status. priority is used to determine if soundeffects should muscle
	;out other sound effects when the channel limit has been reached.
	;TRUE Channel is reserved and cannot be selected.
	;FALSE Channel is reserved and can be selected.
	GetReserved(channel) {
		return dllcall(this._GetReserved, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_GetSurround.html
	;Returns the surround sound status of the specified channel.
	GetSurround(channel) {
		return dllcall(this._GetSurround, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_GetVersion.html
	;Returns the FMOD version number.
	GetVersion() {
		return dllcall(this._GetVersion,Float)
	}
	
	;https://documentation.help/fmod/FSOUND_GetVolume.html
	;Returns the linear volume of the specified channel between 0 and 255
	;On success, the following values are returned : 0 = silent to 255 = full volume.
	;On failure, 0 is returned. To quailfy if this is a real error, call GetLastError().
	GetVolume(channel) {
		return dllcall(this._GetVolume, Int,channel)
	}
	
	;https://documentation.help/fmod/FSOUND_Init.html
	;Initializes the FMOD Sound System.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	Init(mixRate=44100,maxChannels=32,flags=0) {
		return dllcall(this._Init, Int,mixRate, Int,maxChannels, UInt,flags, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_IsPlaying.html
	;Returns if the channel is currently playing or not.
	;TRUE channel is currently active and playing
	;FALSE channel is currently idle.
	IsPlaying(channel) {
		return dllcall(this._IsPlaying, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_PlaySound.html
	;Plays a sample in a specified channel, using the sample's default frequency, volume
	;and pan settings.
	;On success, the channel handle that was selected is returned.
	;On failure, -1 is returned.
	PlaySound(samplePointer,channel=-1) {
		return dllcall(this._PlaySound, Int,channel, Ptr,samplePointer)
	}
	
	;https://documentation.help/fmod/FSOUND_PlaySoundEx.html
	;Extended featured version of FSOUND_PlaySound.
	;New functionality includes the ability to start the sound paused.
	;This allows attributes of a channel to be set freely before the sound actually starts playing, until FSOUND_SetPaused(FALSE) is used.
	;Also added is the ability to associate the channel to a specified DSP unit. This allows the user to 'group' channels into seperate DSP units, which allows effects to be inserted between these 'groups', and allow various things like having one group affected by reverb (wet mix) and another group of channels unaffected (dry).
	;This is useful to seperate things like music from being affected by DSP effects, while other sound effects are.
	;On success, the channel handle that was selected is returned.
	;On failure, -1 is returned.
	PlaySoundEx(samplePointer,channel=-1,dspUnit=0,startPaused=0) {
		return dllcall(this._PlaySoundEx, Int,channel, Ptr,samplePointer, Ptr,dspUnit, Char,startPaused)
	}
	
	;https://documentation.help/fmod/FSOUND_Record_GetDriver.html
	;Returns the currently selected recording driver number. Drivers are enumerated when selecting a driver
	;with FSOUND_Record_SetDriver or other driver related functions such as FSOUND_Record_GetNumDrivers or
	;FSOUND_Record_GetDriverName
	RecordGetDriver() {
		return dllcall(this._RecordGetDriver)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_Free.html
	;Removes a sample from memory and makes its slot available again.
	SampleFree(sample) {
		dllcall(this._SampleFree, Ptr,sample)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_Get.html
	;Returns a pointer to a managed sample based on the index passed.
	SampleGet(sampleIndex) {
		return dllcall(this._SampleGet, Int,sampleIndex, Ptr)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_GetDefaults.html
	;Returns the default volume, frequency, pan and priority values for the specified sample.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SampleGetDefaults(sample,ByRef frequency,ByRef volume,ByRef pan, ByRef priority) {
		return dllcall(this._SampleGetDefaults, Ptr,sample, IntP,frequency, IntP,volume, IntP,pan, IntP,priority, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_GetLength.html
	;Returns the length of the sample in SAMPLES
	;On success, the length of sample in SAMPLES is returned.
	;On failure, 0 is returned.
	SampleGetLength(sample) {
		return dllcall(this._SampleGetLength, Ptr,sample, UInt)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_GetMinMaxDistance.html
	;Get the minimum and maximum audible distance for a sample.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SampleGetMinMaxDistance(sample,ByRef min,Byref max) {
		return dllcall(this._SampleGetMinMaxDistance, Ptr,sample, FloatP,min, FloatP,max, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_GetName.html
	;Returns a pointer to a NULL terminated string containing the sample's name.
	;On success, the name of the sample is returned.
	;On failure, NULL is returned.
	SampleGetName(sample) {
		strPointer := dllcall(this._SampleGetName, Ptr,sample, Ptr)
		if (strPointer)
			return StrGet(strPointer,,"UTF-8")
		return 0
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_Load.html
	;Loads and decodes a static soundfile into memory.
	;This includes such files as .WAV, .MP2, .MP3, .OGG, .RAW and others.
	;On success, a sample pointer is returned.
	;On failure, NULL is returned.
	SampleLoad(file,index=-1) {
		ifnotexist,%file%
		{
			msgbox % "Error: file " file " does not exist!"
			return 0
		}
		filegetsize,size,%file%
		varsetcapacity(buffer,size)
		f := fileopen(file,"r")
		f.rawread(buffer,size)
		f.close()
		
		return dllcall(this._SampleLoad, Int,index, Ptr,&buffer, UInt,0x8000, Int,0, Int,size)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_SetDefaults.html
	;Sets a sample's default attributes, so when it is played it uses these values without having to specify them later.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SampleSetDefaults(sample,frequency,volume,pan,priority) {
		return dllcall(this._SampleSetDefaults, Ptr,sample, Int,frequency, Int,volume, Int,pan, Int,priority, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_SetMaxPlaybacks.html
	;Sets the maximum number of times a sample can play back at once.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SampleSetMaxPlaybacks(sample,max) {
		return dllcall(this._SampleSetMaxPlaybacks, Ptr, sample, Int,max, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_SetMinMaxDistance.html
	;Sets the minimum and maximum audible distance for a sample.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SampleSetMinMaxDistance(sample,min,max) {
		return dllcall(this._SampleSetMinMaxDistance, Ptr,sample, Float,min, Float,max, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Sample_SetMode.html
	;Sets a sample's mode. This can only be FSOUND_LOOP_OFF,FSOUND_LOOP_NORMAL, FSOUND_LOOP_BIDI or FSOUND_2D.
	;You cannot change the description of the contents of a sample or its location. 
	;FSOUND_2D will be ignored on the Win32 platform if FSOUND_HW3D was used to create the sample.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	;0x1		FSOUND_LOOP_OFF	  		For non looping samples. 
	;0x2		FSOUND_LOOP_NORMAL	  	For forward looping samples. 
	;0x4		FSOUND_LOOP_BIDI	 	For bidirectional looping samples. (no effect if in hardware). 	
	;0x2000 	FSOUND_2D				Tells software (not hardware) based sample not to be included in 3d processing.
	SampleSetMode(sample,mode) {
		return dllcall(this._SampleSetMode, Ptr,sample, UInt,mode, Char)
	}
	
	
	;https://documentation.help/fmod/FSOUND_SetCurrentPosition.html
	;Sets the current position of the sound in SAMPLES not bytes.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetCurrentPosition(channel,position) {
		return dllcall(this._SetCurrentPosition, Int,channel, UInt,position, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetFrequency.html
	;Sets a channels frequency or playback rate, in HZ.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetFrequency(channel,frequency) {
		return dllcall(this._SetFrequency, Int,channel, Int,frequency, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetHWND.html
	;This is an optional function to set the window handle of the application
	;you are writing, so Directsound can tell if it is in focus or not.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetHWND(hwnd) {
		return dllcall(this._SetHWND, Ptr,hwnd, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetLoopMode.html
	;Sets the loop mode for a particular CHANNEL, not sample.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	;0x1		FSOUND_LOOP_OFF	  		For non looping samples. 
	;0x2		FSOUND_LOOP_NORMAL	  	For forward looping samples. 
	;0x4		FSOUND_LOOP_BIDI	 	For bidirectional looping samples. (no effect if in hardware). 	
	;0x2000 	FSOUND_2D				Tells software (not hardware) based sample not to be included in 3d processing.
	SetLoopMode(channel,loopMode=2) {
		return dllcall(this._SetLoopMode, Int,channel, UInt,loopMode, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetMinHardwareChannels.html
	;This sets the minimum allowable hardware channels before FMOD drops back to 100 percent software.
	;This is helpful for minimum spec cards, and not having to guess how many hardware channels
	;they might have. This way you can guarantee and assume a certain number of channels for
	;your application and place them all in FSOUND_HW3D without fear of the playsound failing
	;because it runs out of channels on a low spec card.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetMinHardwareChannels(min) {
		return dllcall(this._SetMinHardwareChannels, Int,min, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetMute.html
	;Mutes and un-mutes a channel.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetMute(channel,muteState) {
		return dllcall(this._SetMute, Int,channel, Char,muteState, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetPan.html
	;Sets a channels pan position linearly
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetPan(channel,pan) {
		return dllcall(this._SetPan, Int,channel, Int,pan, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetPaused.html
	;Pauses or unpauses a sound channel.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetPaused(channel,pausedState) {
		return dllcall(this._SetPaused, Int,channel, Char,pausedState, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetPriority.html
	;Sets a channels priority. Higher priority means it is less likely to get discarded when
	;FSOUND_FREE is used to select a channel, when all channels are being used, and one has to
	;be rejected. If a channel has an equal priority then it will be replaced.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetPriority(channel,priority) {
		return dllcall(this._SetPriority, Int,channel, Int,priority, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetReserved.html
	;This sets the reserved status of a channel. Reserving a channel is related to setting its
	;priority, but reserving a channel means it can NEVER be stolen by a channel request. It
	;could be thought of as an extra high priority, but is different in that reserved channels do
	;not steal from each other, whereas channels with equal priorities do (unless there are
	;channels with lower priorities that it can steal from). If all channels were reserved and
	;another request for came in for a channel, it would simply fail and the sound would not be played.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetReserved(channel,reservedState) {
		return dllcall(this._SetReserved, Int,channel, Char,reservedState)
	}
	
	;https://documentation.help/fmod/FSOUND_SetSurround.html
	;Sets a channels surround sound status. This surround sound is a fake dolby trick that
	;effectively pans the channel to the center, but inverts the waveform in one speaker to
	;make it sound fuller or spacier, or like it is coming out of space between the 2 speakers.
	;Panning is ignored while surround is in effect.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetSurround(channel,surroundState) {
		return dllcall(this._SetSurround, Int,channel, Char,surroundState, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetVolume.html
	;Sets a channels volume linearly.
	;This function IS affected by FSOUND_SetSFXMasterVolume.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetVolume(channel,volume) {
		return dllcall(this._SetVolume, Int,channel, Int,volume, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_SetVolumeAbsolute.html
	;Sets a channels volume linearly.
	;This function is NOT affected by master volume.
	;This function is used when you want to quiet everything down using FSOUND_SetSFXMasterVolume, but make
	;a channel prominent.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	SetVolumeAbsolute(channel,volume) {
		return dllcall(this._SetVolumeAbsolute, Int,channel, Int,volume, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_StopSound.html
	;Stops a specified sound channel from playing, and frees it up for re-use.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StopSound(channel) {
		return dllcall(this._StopSound, Int,channel, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_Close.html
	;Shuts down and releases an FSOUND stream.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StreamClose(stream) {
		return dllcall(this._StreamClose, Ptr,stream, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetLengthMs.html
	;Returns the size of the stream in MILLISECONDS.
	;On success, the size of the stream in MILLISECONDS is returned.
	;On failure, 0 is returned.
	StreamGetLengthMs(stream) {
		return dllcall(this._StreamGetLengthMs, Ptr,stream)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetLength.html
	;Returns the size of the stream in BYTES.
	;On success, the size of the stream in BYTES is returned.
	;On failure, 0 is returned.
	StreamGetLength(stream) {
		return dllcall(this._StreamGetLength, Ptr,stream)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetOpenState.html
	;If a stream is opened with FSOUND_NONBLOCKING, this function returns the state of the opening stream.
	; 0 = stream is opened and ready.
	;-1 = stream handle passed in is invalid.
	;-2 = stream is still opening or performing a SetSubStream command.
	;-3 = stream failed to open. (file not found, out of memory or other error).
	;-4 = connecting to remote host (internet streams only)
	;-5 = stream is buffering data (internet streams only)
	StreamGetOpenState(stream) {
		return dllcall(this._StreamGetOpenState, Ptr,stream)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetPosition.html
	;Returns the current FILE position of the stream of the stream in BYTES.
	;On success, the current stream's position in BYTES is returned.
	;On failure, 0 is returned.
	StreamGetPosition(stream) {
		return dllcall(this._StreamGetPosition, Ptr,stream, UInt)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetSample.html
	;Returns the FSOUND_SAMPLE definition that the stream uses internally.
	;You can use this to get a variety of information like the songs name, default speed and more.
	;On success, a handle to the FSOUND_SAMPLE definition is returned.
	;On failure, 0 is returned.
	StreamGetSample(stream) {
		dllcall(this._StreamGetSample, Ptr,stream, Ptr)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_GetTime.html
	;Returns the current time offset in stream in milliseconds.
	;On success, the current stream's position in milliseconds is returned.
	;On failure, 0 is returned.
	StreamGetTime(stream) {
		return dllcall(this._StreamGetTime, Ptr,stream)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_Open.html
	;Opens an audio file/url/cd ready for streaming.
	;This opens the file in preparation for playback in real-time, without needing to decode the whole file into memory first.
	;On success, a pointer to an opened stream is returned.
	;On failure, NULL is returned.
	StreamOpen(file,mode=0,offset=0,length=0) {
		return dllcall(this._StreamOpen, AStr,file, UInt,mode, Int,offset, Int,length)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_Play.html
	;Starts a pre-opened stream playing.
	;On success, the channel handle the stream is playing in is returned.
	;On failure, -1 is returned.
	StreamPlay(stream,channel=-1) {
		return dllcall(this._StreamPlay, Int,channel, Ptr,stream)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_PlayEx.html
	;Extended featured version of FSOUND_Stream_Play.
	;Added functionality includes the ability to start the stream paused. This allows attributes
	;of a stream channel to be set freely before the stream actually starts playing, until FSOUND_SetPaused(FALSE) is used.
	;Also added is the ability to associate the stream channel to a specified DSP unit. This allows
	;the user to 'group' channels into seperate DSP units, which allows effects to be inserted
	;between these 'groups', and allow various things like having one group affected by reverb (wet mix) and another group of
	;channels unaffected (dry). This is useful to seperate things like music from being affected
	;by DSP effects, while other sound effects are.
	;On success, a channel handle the stream is playing in is returned.
	;On failure, -1 is returned.
	StreamPlayEx(stream,channel=-1,dspUnit=0,paused=0) {
		return dllcall(this._StreamPlayEx, Int,channel, Ptr,stream, Ptr,dspUnit, Char paused)
	}
	
	StreamSetEndCallback(stream,callback,data=0) {
		return dllcall(this._StreamSetEndCallback, Ptr,stream, Ptr,callback, Int,data, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_SetLoopCount.html
	;Sets the stream to loop the number of times specified by the user. If not called it loops forever.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StreamSetLoopCount(stream,count) {
		return dllcall(this._StreamSetLoopCount, Ptr,stream, Int,count, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_SetMode.html
	;Set a streams mode.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	;MODES
	;0x1		FSOUND_LOOP_OFF	  		For non looping samples. 
	;0x2		FSOUND_LOOP_NORMAL	  	For forward looping samples. 
	;0x4		FSOUND_LOOP_BIDI	 	For bidirectional looping samples. (no effect if in hardware). 	
	;0x2000 	FSOUND_2D				Tells software (not hardware) based sample not to be included in 3d processing.
	StreamSetMode(stream,mode) {
		return dllcall(this._StreamSetMode, Ptr,stream, UInt,mode, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_SetPosition.html
	;Sets the current stream's FILE position in BYTES.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StreamSetPosition(stream,position) {
		return dllcall(this._StreamSetPosition, Ptr,stream, UInt,position, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_SetTime.html
	;Sets the current stream's FILE position in MILLISECONDS.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StreamSetTime(stream,ms) {
		return dllcall(this._StreamSettime, Ptr,stream, Int,mode, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Stream_Stop.html
	;Stops a stream from playing.
	;On success, TRUE is returned.
	;On failure, FALSE is returned.
	StreamStop(stream) {
		dllcall(this._StreamStop, Ptr,stream, Char)
	}
	
	;https://documentation.help/fmod/FSOUND_Update.html
	;This updates the 3d sound engine and DMA engine (only on some platforms), and should be called once a game frame.
	;This function will also update the software mixer if you have selected FSOUND_OUTPUT_NOSOUND_NONREALTIME as your output mode.
	Update() {
		dllcall(this._Update)
	}
	
	;Helper function
	GPA(function) { ;GetProcAddress
		return DllCall("GetProcAddress", Ptr, this.lib, AStr, function, Ptr)
	}

}
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: [Library] fmod fsound wrapper

12 Jun 2020, 16:16

Thank you for sharing this work with us here. It looks very interesting.

I'll give it a try when I get the chance.

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: [Library] fmod fsound wrapper

13 Jun 2020, 12:59

Quite useful, also could be used as an alternative to soundplay on Windows N/KN without media feature pack!
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: [Library] fmod fsound wrapper

29 Jun 2020, 17:57

Is there any possibility this could be done using 64 bit version? (if there actually is a 64 bit fmod.dll)

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

s0ftnauxz
Posts: 17
Joined: 12 Oct 2014, 16:06

Re: [Library] fmod fsound wrapper

16 Mar 2021, 21:57

i try script effects, dont work for me, or maybe i do some wrong?

fsound.FX_SetEcho(sound1, 1)
fsound.SetPan(sound1,Right, 20)
fsound.SetVolume(sound1,10)
fsound.SetFrequency(sound1,8000)

Code: Select all

#noenv
#persistent
#singleinstance,force
#include fmod_fsound.ahk

;downloads 2 example sounds from my dropbox
IfNotExist,file1.wav
	urldownloadtofile,https www.dropbox.com /s/yilpqgv1ypwwemx/file1.wav?dl=1,file1.wav  Broken Link for safety
IfNotExist,file2.wav
	urldownloadtofile,https www.dropbox.com /s/p4l8crryn76rb7d/file2.wav?dl=1,file2.wav  Broken Link for safety

fsound := new fmod_fsound() ;Create new instance of fmod_fsound class
fsound.Init() ;Initialize the Fsound system

sound1 := fsound.StreamOpen("file1.wav") 	;prepare file for streaming
sound2 := fsound.StreamOpen("file2.wav") 	;prepare file for streaming

fsound.StreamSetMode(sound1,2) 				;mode 2 = FSOUND_LOOP_NORMAL
;fsound.StreamSetMode(sound2,2) 				;mode 2 = FSOUND_LOOP_NORMAL

fsound.FX_Enable(sound1,2) 				;mode 2 = FSOUND_LOOP_NORMAL
              
;fsound.FX_SetGargle(sound1,2) 				;mode 2 = FSOUND_LOOP_NORMAL
;fsound.StreamSetLoopCount(sound1,19) 		;loop 19 additional times, 20 total
;fsound.FX_SetChorus(sound1, 1)
fsound.FX_SetEcho(sound1, 1)
fsound.SetPan(sound1,Right, 20)
fsound.SetVolume(sound1,10)
fsound.SetFrequency(sound1,8000)

fsound.StreamPlay(sound1)
fsound.StreamPlay(sound2)

;KeyWait, LAlt, D
;fsound.SetMute(sound1,2)


KeyWait, Esc, D
fsound.StreamStop(sound1) 					;after 3 seconds stop the stream
fsound.StreamStop(sound2) 					;after 3 seconds stop the stream

fsound.StreamClose(sound1) 					;close stream
fsound.SampleFree(sound2) 					;free sample from memory
fsound.Close() 								;shutdown fsound
exitapp

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 157 guests