Code/new Sound Effects Player

From kJams Wiki
Jump to navigation Jump to search

So this has been a very frequent request among kJams users, so I decided to take a break from my other kJams project, and get this thing up and running. Place any sound effects files you want to access in a folder called "soundFX" in your kJams folder (~/Music/kJams, most likely) or you can run the script once and it will make it for you. You have two choices on how to implement this. You can open Applescript Editor and paste the following code into a new script window, and save as an application, and then run as you would any other application. Or better yet, open up Automator, select "Service" from the template choices on the opening splash screen, and then drag "Run AppleScript" from the Library onto the Stage. Change the "Service receives selected" popup to "no input", and "applications" to "kJams". (First choose "other…" from the popup menu, and then browse to kJams in the file selector.) Next, paste the following code over top of where is says "(* Your script goes here *)", and save. That's it, you're ready to go! To use, run the application or select the service from the service menu, and a list of your sound effects will pop up. Select the one you want and hit "Play." Voilá, the sound plays over top of any audio you already have running.

Since this is already version r2, there are some additional features:

1) You can play another effect while the first is still playing. Both will play together. Play the sound of footsteps, and while it is still playing, add the sound of a door closing, layered with a voice saying "See ya!"
2) Want a chorus? Select multiple items from the list at one time and they will all play in unison. I have tested with as many as 36 simultaneous sounds playing.

Let me know what you think back in the forums.

P.S. If you find this useful, check out my other scripts.
P.P.S If you think this is the bomb, and are feeling generous, I could always use the additional incentive :) paypal the author.

<iimg>https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=manifold_sky%yahoo%2ecom&item_name=kJams%20Video%20Backdrop%20Donation&no_shipping=2&no_note=1&currency_code=USD&lc=US&bn=PP%2dBuyNowBF&charset=UTF%2d8 !paypal.gif</iimg>

Code:


set soundFXFolder to ((path to music folder) as string) & "kJams:"
tell application "Finder"
	try
		make new folder at soundFXFolder with properties {name:"SoundFX"}
		display dialog "SoundFX folder created. Have fun!" buttons {"Rockin'!"} default button 1
		return
	end try
end tell
set soundFXFolder to soundFXFolder & "soundFX:"
set soundFX to list folder soundFXFolder without invisibles

-- get the sound effect from the list
try
	set soundFX to item 1 of {choose from list soundFX with prompt "Pick a sound:" with title "Sound Effects" OK button name "Play" cancel button name "Abort" with multiple selections allowed}
on error
	--if the FX folder is empty
	display dialog "Your SoundFX folder is empty. To play sound effects, copy audio files into ~/Music/kJams/soundFX"
end try

repeat with soundFile in soundFX
	set soundFile to the quoted form of the POSIX path of (soundFXFolder & soundFile)
	do shell script ("afplay " & soundFile & " > /dev/null 2>&1 &")
end repeat

If you want the player to hang around, ready to play at all times, use this modified code:

set soundFXFolder to ((path to music folder) as string) & "kJams:"
tell application "Finder"
	try
		make new folder at soundFXFolder with properties {name:"SoundFX"}
		display dialog "SoundFX folder created. Have fun!" buttons {"Rockin'!"} default button 1
		return
	end try
end tell
set soundFXFolder to soundFXFolder & "soundFX:"
-- set soundFX to true
set soundFXlist to list folder soundFXFolder without invisibles
repeat
	-- get the sound effect from the list
	try
		set soundFX to item 1 of {choose from list soundFXlist with prompt "Pick a sound:" with title "Sound Effects" OK button name "Play" cancel button name "Abort" with multiple selections allowed}
		if soundFX is false then return
	on error
		--if the FX folder is empty
		display dialog "Your SoundFX folder is empty. To play sound effects, copy audio files into ~/Music/kJams/soundFX"
	end try
	
	repeat with soundfile in soundFX
		set soundfile to the quoted form of the POSIX path of (soundFXFolder & soundfile)
		do shell script ("afplay " & soundfile & " > /dev/null 2>&1 &")
	end repeat
end repeat