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

« 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)
# status.py
from graphics import *
import fgimage
__all__ = ["status_on", "status_off"]

bar = Image.new((119,13))
bgcolor = screenshot().getpixel((0,0))[0]
fg = fgimage.FGImage()

def status_on(message=None):
    if message is not None:
        bar.clear(bgcolor)
        bar.text((0,11), unicode(message), 0xffffff)
    fg.set(57,30, bar._bitmapapi())

def status_off():
    fg.unset()

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

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.
# from http://logic-users.org/forums/L-OT/295
# Each note's frequency is 2^(1/12) times of the previous note.

freq = 440 * 2^((n-69)/12)
n = 69 + 12*log(freq/440)/log(2)

# Doe, ray, me, fa, sol, la, tee, doe
>>> play([(i, 100) for i in [60, 62, 64,65, 67, 69, 71,72]])

appuifw.note allow global note in pys60 1.3.1

# note in 1.2 
note(text, type)
# note in 1.3.1
note(text[, type[, global]])

type = 'info' (default) or 'error', 'conf'
global = 1 # display even if app is in background
import appuifw
appuifw.note(u'Hello World')             # simplest case
appuifw.note(u'Cannot connect', 'error') # show error
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.
import fgimage, e32

img = Image.new((100, 16))  # create a notification text
img.text((0, 14), u'Hello world', 0)

fg = fgimage.FGImage()
x, y = 0, 40           # position to draw
fg.set(x, y, img._bitmapapi())  # send it

e32.ao_sleep(1)
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.
>>> import smidi
>>> m = smidi.MidiOutFile('C:\\out.mid')
>>> m.header()
>>> m.start_of_track()
>>> m.update_time(0)
>>> m.note_on(note=0x40)  # single note
>>> m.update_time(192)
>>> m.note_off(note=0x40) # stop it after 192
>>> m.update_time(0)
>>> m.end_of_track()
>>> m.eof()

>>> from audio import Sound
>>> s = Sound.open('C:\\out.mid')
>>> s.play()
>>> 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.
>>> from smidi import play
>>> play([(64,192), (32, 192)])

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