Code Monkey home page Code Monkey logo

Comments (6)

JupiterLikeThePlanet avatar JupiterLikeThePlanet commented on June 2, 2024

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 /bubble-man.mp3"
music_off:
Process.kill(15, $pid)

Any ideas where to begin testing on something like this?

from goby.

nskins avatar nskins commented on June 2, 2024

Hey @JupiterLikeThePlanet - 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.

from goby.

JupiterLikeThePlanet avatar JupiterLikeThePlanet commented on June 2, 2024

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

from goby.

nskins avatar nskins commented on June 2, 2024

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.

from goby.

JupiterLikeThePlanet avatar JupiterLikeThePlanet commented on June 2, 2024

So I'm pushing up some code I've added, but the task still has awhile until being completed. Still needs to be incorporated into game, play on first player move, and stop during an event. 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.

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. At the moment, for afplay intro.mp3, use an absolute path to the song.

	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)

from goby.

JupiterLikeThePlanet avatar JupiterLikeThePlanet commented on June 2, 2024

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. I have not incorporated this into the current PR as it yields the same result.

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 will be killed immediately only if the bash code called executes once. I've tried using Signal class with trap method in several ways with no luck:

#1: Signal.trap("HUP") { exec 'afplay', "#{_relativePATH}/intro.mp3" }
#2: Signal.trap("HUP") { "while #{@music} = true; do afplay #{_relativePATH}/intro.mp3; done" }
#3: Signal.trap("HUP") { fork{ exec 'afplay', "#{_relativePATH}/intro.mp3" } }	

Process.kill("HUP", $pid)

Besides HUP, I've tried others including KILL, but nothing has yielded the desired result. 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.

from goby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.