Jump to content

Environment generator with external control


DrJeseuss
 Share

Recommended Posts

This is an environment generator which will allow input control from external sources. The code is in the works and I'll update this thread as work is completed. Any input is welcome, and hopefully this project will be useful or helpful to others with a similar interest.

Features to include:

-multiple 'channels' of audio playing at the same time

-ability to play a channel once (called 'random' or as a loop (called 'background')

-support for multiple common audio formats (WAV, MP3, OGG, etc)

-random and background channels to have thresholds for min/max volume (actual value random between)

-background volume changes to occur within a set time (less than x) at random ('Rate of Change')

-background volume changes will 'slide' over time for smooth transition

-random events to occur within a set time (less than x) at random ('Frequency')

-ability to save/load 'scenes' (preset files, thresholds, etc)

-smooth transition between scenes when a different scene is loaded

-input from external sources (XML and serial for now)

-inputs can trigger events such as:

-turn on/off 'background' or 'random' channels

-play 'random' sound

-adjust thresholds for 'background' or 'random' channel

-adjust 'Frequency' of 'random' channel

-adjust 'Rate of Change' for 'background' channel

-load 'Scene'

As an example of usage for a haunted house, you could have a loop of wind howling, and another of a bubbling cauldren. 'random' events might include a wolf howl or bats flying by. Then, using s serial device, you could detect a person approaching and play a witch's laugh. Another sensor could detect a person stepping (pressure sensor) and play a loud scream... etc.

Once I have the skeleton code written I'll post it here, then update over time as features are added and developed.

Link to comment
Share on other sites

I'm sure this is all well intended, but please write some code first before starting a thread here in example scripts. :D

Fair enough, though the code is VERY rough and likely to change dramatically to fit in the overall script. I have the building blocks and am now in the process of stitching things together into a working 'beta'. Here are a few of the fundamental blocks I'll be using:

This bit demonstrates generating a random time within a set range, then checking to see if the time has elapsed yet. This will not be completely accurate, though for the purposes of a 'random' generator it'll work nicely. Things are done this way as there will be a fair bit of other work going on between setting the "go time" and checking it, then looping around again. I'm sure it could be done differently, and in time, it may be.

#include <Date.au3>
#region  ; Random Timer Example
$RandomMin = 0    ; seconds   (60/minute, 3600/hour, 86400/day
$RandomMax = 20 ; seconds
$tTime = _NowCalc()
ConsoleWrite($tTime & @CRLF)
$GoTime = _DateAdd("s", Random($RandomMin,$RandomMax,1), $tTime)
ConsoleWrite($GoTime & @CRLF)
$TimeDifference = _DateDiff( 's',_NowCalc(),$GoTime)
ConsoleWrite($TimeDifference & @CRLF)
While $TimeDifference >= 0
$TimeDifference = _DateDiff( 's',_NowCalc(),$GoTime)
ConsoleWrite($TimeDifference & @CRLF)
Sleep(1000)
WEnd
ConsoleWrite("Go Time!" & @CRLF)
Exit
#endregion

This bit demonstrates using BASS to load 2 files and loop them at the same time, then 'playing' with the volumes during playback. This will be a key component of making the 'background' loop files work. The random timer above will trigger changes to the channel volume levels within a threshold and at an interval chosen by the user. This is a similar structure to what will fire the 'random' channels, again using the above random timer with user set thresholds to determine when to play these events. This will be used in pieces in the main body of the script.

#include <Array.au3>
#include <Bass.au3>
#include <BassConstants.au3>
#region  ; Sound Channel Example
#region ; Initialize BASS
Global $playing_state = -1
;Open Bass.DLL.  Required for all function calls.
_BASS_STARTUP ("BASS.dll")
;Initalize bass.  Required for most functions.
_BASS_Init(0, -1, 44100, 0, "")
;Check if bass iniated.  If not, we cannot continue.
If @error Then
ConsoleWrite("Could not initialize audio" & @CR & "Error = " & @error)
Exit
EndIf
#endregion
#region  ; Create streams
;Prompt the user to select a MP3 file
$file1 = FileOpenDialog("Open...", "", "WAV Files (*.WAV)")
;Create a stream from that file.
$MusicHandle1 = _BASS_StreamCreateFile(False, $file1, 0, 0, $BASS_SAMPLE_LOOP)
;Check if we opened the file correctly.
If @error Then
ConsoleWrite("Could not load audio file" & @CR & "Error = " & @error)
Exit
EndIf
;Prompt the user to select a MP3 file
$file2 = FileOpenDialog("Open...", "", "WAV Files (*.wav)")
;Create a stream from that file.
$MusicHandle2 = _BASS_StreamCreateFile(False, $file2, 0, 0, $BASS_SAMPLE_LOOP)
;Check if we opened the file correctly.
If @error Then
ConsoleWrite("Could not load audio file" & @CR & "Error = " & @error)
Exit
EndIf
#endregion
;Iniate playback
_BASS_ChannelSetAttribute($MusicHandle1, $BASS_ATTRIB_VOL, 0) ; Begin playback with volume at 0 for smooth startup
_BASS_ChannelSetAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0)
_BASS_ChannelPlay($MusicHandle1, 1)
;Sleep(1000)
_BASS_ChannelPlay($MusicHandle2, 1)
ConsoleWrite("Playing" & @CRLF)
Sleep(1000)
_BASS_ChannelSlideAttribute($MusicHandle1, $BASS_ATTRIB_VOL, 0.65, 8000)
;_BASS_ChannelSlideAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0.45, 7000)
ConsoleWrite("VolumeSlide up started" & @CRLF)
Sleep(10000)
_BASS_ChannelSetAttribute($MusicHandle1, $BASS_ATTRIB_VOL, 0)
_BASS_ChannelSetAttribute($MusicHandle2, $BASS_ATTRIB_VOL, .7)
ConsoleWrite("Volume set to 0" & @CRLF)
Sleep(2000)
_BASS_ChannelSlideAttribute($MusicHandle1, $BASS_ATTRIB_VOL, .8, 8000)
_BASS_ChannelSlideAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0.25, 8000)
ConsoleWrite("VolumeSlide up started" & @CRLF)
Sleep(10000)
_BASS_ChannelSlideAttribute($MusicHandle1, $BASS_ATTRIB_VOL, 0, 8000)
Sleep(8000)
_BASS_ChannelStop($MusicHandle1)
ConsoleWrite("Stop 1" & @CRLF)
Sleep(2000)
_BASS_ChannelSlideAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0.15, 4000)
ConsoleWrite("VolumeSlide down started" & @CRLF)
Sleep(4000)
_BASS_ChannelSlideAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0.85, 4000)
ConsoleWrite("VolumeSlide up started" & @CRLF)
Sleep(4000)
_BASS_ChannelSlideAttribute($MusicHandle2, $BASS_ATTRIB_VOL, 0, 2000)
Sleep(2000)
_BASS_ChannelStop($MusicHandle2)
ConsoleWrite("Stop 2" & @CRLF)
Sleep(1000)
_BASS_Free()
Exit
Func OnAutoItExit()
;Free Resources
_BASS_Free()
EndFunc   ;==>OnAutoItExit
#endregion

I'm currently working on a very basic GUI to begin putting the pieces into action. This should be soon to follow. Enjoy.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...