Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Showing message on status bar

There's an empty space below app.title that we can
use as a status bar. So, I modify this recipe and
make it into a module. (need fgimage.pyd from here)
   1  
   2  # status.py
   3  from graphics import *
   4  import fgimage
   5  __all__ = ["status_on", "status_off"]
   6  
   7  bar = Image.new((119,13))
   8  bgcolor = screenshot().getpixel((0,0))[0]
   9  fg = fgimage.FGImage()
  10  
  11  def status_on(message=None):
  12      if message is not None:
  13          bar.clear(bgcolor)
  14          bar.text((0,11), unicode(message), 0xffffff)
  15      fg.set(57,30, bar._bitmapapi())
  16  
  17  def status_off():
  18      fg.unset()

You can use it easily this way
   1  
   2  >>> from status import *
   3  >>> status_on('Hello world')  # show it
   4  >>> status_off()              # hide it
   5  >>>

See a screenshot.

MIDI Note number and frequency

Summary of MIDI Note Numbers
MIDI Note to Frequency

I can use them with my midi snippet.
   1  
   2  # from http://logic-users.org/forums/L-OT/295
   3  # Each note's frequency is 2^(1/12) times of the previous note.
   4  
   5  freq = 440 * 2^((n-69)/12)
   6  n = 69 + 12*log(freq/440)/log(2)
   7  
   8  # Doe, ray, me, fa, sol, la, tee, doe
   9  >>> play([(i, 100) for i in [60, 62, 64,65, 67, 69, 71,72]])

appuifw.note allow global note in pys60 1.3.1

   1  
   2  # note in 1.2 
   3  note(text, type)
   4  # note in 1.3.1
   5  note(text[, type[, global]])

type = 'info' (default) or 'error', 'conf'
global = 1 # display even if app is in background
   1  
   2  import appuifw
   3  appuifw.note(u'Hello World')             # simplest case
   4  appuifw.note(u'Cannot connect', 'error') # show error
   5  appuifw.note(u'New message', 'info', 1)  # alert from background

Using foreground image

You can draw directly to the phone screen.
It is useful for a notification from a background program.
You need to install fgimage.pyd from here.
   1  
   2  import fgimage, e32
   3  
   4  img = Image.new((100, 16))  # create a notification text
   5  img.text((0, 14), u'Hello world', 0)
   6  
   7  fg = fgimage.FGImage()
   8  x, y = 0, 40           # position to draw
   9  fg.set(x, y, img._bitmapapi())  # send it
  10  
  11  e32.ao_sleep(1)
  12  fg.unset()                # then clear it


See (a bit) more discussion here.

Generate and play midi on mobile phone

From a previous snippet, you can play any sound file
(including midi) with pys60.
http://bigbold.com/snippets/posts/show/400

Now, what if you can generate a midi file as well?
I found a pure python midi file library.
http://www.mxm.dk/products/public/pythonmidi

I modify it a bit, just to make a single file for easy download.
http://larndham.net/service/pys60/smidi.py
With it, you can play a single note with the following code.
   1  
   2  >>> import smidi
   3  >>> m = smidi.MidiOutFile('C:\\out.mid')
   4  >>> m.header()
   5  >>> m.start_of_track()
   6  >>> m.update_time(0)
   7  >>> m.note_on(note=0x40)  # single note
   8  >>> m.update_time(192)
   9  >>> m.note_off(note=0x40) # stop it after 192
  10  >>> m.update_time(0)
  11  >>> m.end_of_track()
  12  >>> m.eof()
  13  
  14  >>> from audio import Sound
  15  >>> s = Sound.open('C:\\out.mid')
  16  >>> s.play()
  17  >>> s.close()

These 10 lines can be made into a simple function.
Then, you can just type a line and play any note you like
on you mobile phone.
   1  
   2  >>> from smidi import play
   3  >>> play([(64,192), (32, 192)])

Ah... so pythonic. ^_^
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS