opened
nskins
username_0
It would be really cool if we could implement background music in our framework. This would need to support at least the following two features:

1. BGM variable for the Map class that starts playing once the player moves onto the map.
2. Some way to start and stop BGM through Events.

The music should repeat naturally.
BGM mechanic
issue
created
JupiterLikeThePlanet
username_1
I'm giving this a try.  Now, I've found a way to play music and stop it (still working it out), but I'm not certain how to create an RSpec for this.  I think because the commands I'm using are bash commands done through ruby.

_music_on_: 
`$pid = Process.spawn "afplay /Users/jupiterbaudot/Desktop/bubble-man.mp3"`
_music_off_:
`Process.kill(15, $pid)`

Any ideas where to begin testing on something like this?
comment
created
nskins
username_0
Hey @username_1 - I'm glad you'd like to work on this. I'm actually not too sure about the direction or the tests... perhaps the testing will be mostly manual? I will create a new branch `bgm` to which you can make a pull request whenever you're ready.

Not sure if it will be important, but I'd like to first support .midi files since these can be easily generated by various music composition programs. Also, we will need to be a way to distinguish between the OS (main support right now is Ubuntu and OS X) to determine the correct audio player / command to run.
comment
created
JupiterLikeThePlanet
username_1
On another note, I'm trying to figure out what rhs and how this method works.  Could you elaborate on it?
```
# @param [Event] rhs the event on the right.
  def ==(rhs)
    @command == rhs.command
  end
```
comment
created
nskins
username_0
Yes, this may have relevance to #52 (another useless equality operator). It basically just says two Events are equal if they are called by the same command.
comment
created
JupiterLikeThePlanet
username_1
So I'm pushing up some code I've added, but the task still has awhile until being completed.  Just pushing it up now for you to look over while I do some other work during the week.  Here's a run down of what I've done.  I've added an mp3 (which of course can be changed) and all changes here are limited to _map.rb_.  In the code itself:

**class Map**
@music attribute has been added to take a boolean value
```
	def initialize(params = {})
		@name = params[:name] || "Map"
		@tiles = params[:tiles] || [ [Tile.new ] ]
		@regen_location = params[:regen_location] || Couple.new(0,0)
                
		@music = true 
	end

```

**method play_music**
Accepts a boolean value as parameter and sets @music to that value.  I foresee some refactoring here. Anyway, it will play and loop the music if true, kill it if false.
```
	def play_music(bool)
		@music = bool
		if @music === true
			$pid = Process.spawn "while true; do afplay intro.mp3; done"
		elsif @music === false
			# Use sleep for testing music off.  Music will continue to play until it hits the end of track
			# sleep(1)
			Process.kill(15, $pid)
		else
			p "Please enter a true or false parameter"
		end
	end
```
_Process_ is a ruby class but within the string is a bash command that runs in terminal.  When troubleshooting, keep in mind that the error may not be a ruby issue, but perhaps a bash one.

**Testing**
For manually testing this individual file (will need to comment out @tiles &@regen_locations in initialize), run ruby map.rb in command line.  It should play the song once, then kill it.  If you want to test how it loops, edit the _play_music_ method to sleep for, let's say, 15 seconds to see it loop three times [that is if you're using the 5 second clip I've uploaded] 
```
@map = Map.new
@map.play_music(true)
@map.play_music(false)
```
comment
created
JupiterLikeThePlanet
username_1
Updated the PR, see for notes.  I also just quickly tried some other code this morning before I left my place, but alas I'm still having the same issue of the music needing to play out until it finally stops.  
I initialized `@music` as an empty string.  I split the music into 2 methods. 

```
def initialize(params = {})
	@name = params[:name] || "Map"
	@tiles = params[:tiles] || [ [Tile.new ] ]
	@regen_location = params[:regen_location] || Couple.new(0,0)
	@music = ''
end

def play_music
	_relativePATH = File.expand_path File.dirname(__FILE__)
	@music = _relativePATH + '/intro.mp3'
	$pid = Process.spawn "while [ #{@music} != '' ] ; do afplay #{@music}; done"
end

def stop_music
	@music = ''
	Process.kill(15, $pid)
end
```

The issue I'm having occurs at `$pid = Process.spawn "while [ #{@music} != '' ] ; do afplay #{@music}; done"`.  It would seem that whatever needs to cut off the music immediately needs to happen here.  Using on a ruby loop as an alternative hasn't worked for me because the game won't move forward until the loop is exited.   Also, the song can be killed immediately if the bash code called executes once.  So, I'm just at a bit of an impasse here.  If you have time, try it out and let me know if you think.
comment
closed
nskins
username_0
issue