pygmusic
index
/home/kragen/devel/pygmusic/pygmusic.py

Pygmusic: A simple music sequencer in PyGame.  By Kragen Sitaker,
2007-12-07, 08, and 09.
 
Drag things around with the mouse; the right mouse button exits.
 
I, the copyright holder of this work, hereby release it into the
public domain. This applies worldwide.
 
In case this is not legally possible, I grant any entity the right
to use this work for any purpose, without any conditions, unless
such conditions are required by law.

 
Modules
       
Numeric
math
os
pygame
sys
time

 
Classes
       
NumericHaloMovie
Profiler
Visible
ImageDisplay
DragSource
Sound
Trigger
Trash
UglyHalo
NumericHalo
NumericHalo
Timer
RepeatingTimer
World

 
class DragSource(ImageDisplay)
    Here is a source you can drag new Visibles from.
 
 
Method resolution order:
DragSource
ImageDisplay
Visible

Methods defined here:
__init__(self, pos, image, instance)
instance is a callable to call with (x, y) to make the new thing.
handle_click(self, world, ev)
Start a drag with a new instance of, say, a Sound.

Methods inherited from ImageDisplay:
draw(self, world, surface)
ImageDisplay.draw just blits the object's image.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class ImageDisplay(Visible)
    A visible object that displays by merely blitting an image.
 
  Methods defined here:
__init__(self, pos, image)
draw(self, world, surface)
ImageDisplay.draw just blits the object's image.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class NumericHalo(Visible)
    Draws a fading halo computed with Numerical Python.
 
  Methods defined here:
__init__(self, rect)
rect is the area to draw the halo around.
draw(self, world, surface)
Blits the best frame for the halo's current age; possibly suicides.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class NumericHaloMovie
    Renders frames of a halo on demand, then caches them.
 
  Methods defined here:
__getitem__(self, framenum)
Get a frame.
__init__(self, args)
render_frame(self, ii)

 
class Profiler
     Methods defined here:
__init__(self)
__str__(self)
note(self, what)
start(self)

 
class RepeatingTimer(Timer)
    A horizontal strip on the screen that plays things in it repeatedly.
 
 
Method resolution order:
RepeatingTimer
Timer
Visible

Methods defined here:
cursor_rects(self, offset)
handle_click(self, world, ev)
Turns the timer on and off when clicked.
time(self)

Methods inherited from Timer:
__init__(self, rect, cycletime, color, active=True, divisions=8)
cycletime is the time to go through the whole cycle.
cursor_rect(self, start, end)
Returns a Rect from pixel offset 'start' to 'end' for a cursor.
draw(self, world, screen)
draw_cursor(self, world, screen)
Draws the white box that represents the currently playing period,
and also plays the sounds found within.
drawvbar(self, screen, offset, color)
Draws a timing mark at a given offset.
drawvbars(self, screen, n, color)
Draw timing marks for odd intervals of every nth of a cycle.
offset(self)
The current position of the cursor for this timer.
trigger(self)
Start playing the sounds within.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class Sound(ImageDisplay)
    A draggable sound that you can put in the tracks.
 
 
Method resolution order:
Sound
ImageDisplay
Visible

Methods defined here:
__init__(self, pos, image, sound)
handle_click(self, world, ev)
handle_drop(self, world)
Handle being dropped; called by the world.
move(self, delta)
play(self, world)
Plays the object's sound and kicks off a halo; called by timer.
start_drag(self, world, ev)

Methods inherited from ImageDisplay:
draw(self, world, surface)
ImageDisplay.draw just blits the object's image.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
is_drop_target_for(self, obj, world)
Default is to do nothing.

 
class Timer(Visible)
    A horizontal strip on the screen that plays things in it when triggered.
 
  Methods defined here:
__init__(self, rect, cycletime, color, active=True, divisions=8)
cycletime is the time to go through the whole cycle.
cursor_rect(self, start, end)
Returns a Rect from pixel offset 'start' to 'end' for a cursor.
cursor_rects(self, offset)
Returns the Rects showing the currently playing period on the screen.
draw(self, world, screen)
draw_cursor(self, world, screen)
Draws the white box that represents the currently playing period,
and also plays the sounds found within.
drawvbar(self, screen, offset, color)
Draws a timing mark at a given offset.
drawvbars(self, screen, n, color)
Draw timing marks for odd intervals of every nth of a cycle.
handle_click(self, world, ev)
offset(self)
The current position of the cursor for this timer.
time(self)
The time since the beginning of the current play cycle.
trigger(self)
Start playing the sounds within.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class Trash(ImageDisplay)
    The trashcan deletes things dropped on it.
 
 
Method resolution order:
Trash
ImageDisplay
Visible

Methods defined here:
is_drop_target_for(self, object, world)
Delete the dropped thing and make a halo.

Methods inherited from ImageDisplay:
__init__(self, pos, image)
draw(self, world, surface)
ImageDisplay.draw just blits the object's image.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class Trigger(Sound)
    A draggable object that you can put in a timer to trigger some
other thing, such as another timer.
 
 
Method resolution order:
Trigger
Sound
ImageDisplay
Visible

Methods defined here:
__init__(self, pos, image, gun)
# XXX currently inherits from Sound so as to be draggable.
play(self, world)

Methods inherited from Sound:
handle_click(self, world, ev)
handle_drop(self, world)
Handle being dropped; called by the world.
move(self, delta)
start_drag(self, world, ev)

Methods inherited from ImageDisplay:
draw(self, world, surface)
ImageDisplay.draw just blits the object's image.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
is_drop_target_for(self, obj, world)
Default is to do nothing.

 
class UglyHalo(ImageDisplay)
    A white square that fades to nothing over 0.6 seconds to show
that something has happened.
 
 
Method resolution order:
UglyHalo
ImageDisplay
Visible

Methods defined here:
__init__(self, rect)
rect is the area to draw the halo around.
draw(self, world, surface)
Blits the halo image with the current opacity; possibly suicides.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class Visible
    Things that get drawn on the screen and maybe handle mouse clicks
or make sounds.
 
These objects can be added to the world with world.add and are
expected to be able to do the following:
- obj.contains((x, y)): tell whether a mouse position is in the object
- obj.handle_click(world, event): handle a mouse button going down
- obj.play(world): make any appropriate sounds
- obj.center(): return (x, y) center to decide when to be played
- obj.draw(self, world, screen): display itself; called every frame.
- obj.is_drop_target_for(self, object, world): handle object being
  dropped on us
 
Additionally, if an object is draggable, it needs to support these:
- obj.move(delta): move by delta x and delta y
- obj.handle_drop(world): finish being dragged
 
In order to use the default implementations of contains and
center, the object needs to have a rect property that acts like a
pygame.Rect.
 
  Methods defined here:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
class World
    Manages the set of stuff you see on the screen and routes
events.  I think this is basically the Smalltalk MVC Controller.
 
  Methods defined here:
__init__(self, screen)
screen is an SDL/PyGame surface to draw on.
add(self, obj)
Call to put a new visible, clickable object on the screen.
add_nonclickable(self, obj)
Like add, but for nonclickable objects above everything.
 
I added this for halos because they were accidentally getting
drawn underneath other screen objects, and they aren't
supposed to impede the clickability of the objects they're
haloing around.
defer(self, task)
Enqueue a task to be done as soon as possible after redraw.
delete(self, obj)
Remove a visible object (clickable or not) from the world.
grab(self, obj, pos)
Start dragging some object.
handle_click(self, ev)
Route a click event to the relevant object.
handle_motion(self, ev)
Handle mouse motion, by doing a mouse drag if needed.
handle_release(self, ev)
Handle a button release event.
object_at(self, pos)
Return the topmost object at pos, or None.
objects_in(self, arect)
Find all the objects whose centers are in a rectangle.
 
This is used by the timer to figure out what to play.
raise_to_top(self, obj)
Move an object to the top of the drawing stack.
redraw(self)
This gets called whenever there's idle time, i.e. each frame.
ungrab(self)
Terminate any drag.

 
make_halo = class NumericHalo(Visible)
    Draws a fading halo computed with Numerical Python.
 
  Methods defined here:
__init__(self, rect)
rect is the area to draw the halo around.
draw(self, world, surface)
Blits the best frame for the halo's current age; possibly suicides.

Methods inherited from Visible:
center(self)
contains(self, (x, y))
handle_click(self, world, ev)
Default is to do nothing.
is_drop_target_for(self, obj, world)
Default is to do nothing.
play(self, world)
Default is to do nothing.

 
Functions
       
diff((x1, y1), (x2, y2))
Take the offset between two 2D points.
get_halo_movie(*args)
Find a requested halo movie.
gray(x)
Return a grayscale color tuple given an argument in [0,256)
main(argv)
Main program.
padd((x1, y1), (x2, y2))
Add an offset to a 2D point.
pixel(masks, components)
Encode an (r, g, b, alpha) tuple according to given masks.
scale(mask, component)
Encode a floating-point color component in [0, 1] with a bitmask.

 
Data
        black = (0, 0, 0)
halo_movies = {}
white = (255, 255, 255)