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-6 of 6 total  RSS 

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]])

Convert a number to its closest fraction

Sometimes, I need to find a ratio approximation of a number.
Like 640 / 480 (vga) or similar number. I learn about farey
series a few years ago (1994).
The idea is actually quite simple.
   1  
   2  >>> farey(math.pi,100)
   3  (22, 7)

Get the implementation here.

Converting Between Different Naming Convetions

From Sami Hangaslammi's recipe.
   1  
   2  import re
   3  
   4  def cw2us(x): # capwords to underscore notation
   5      return re.sub(r'(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])', r"_\g<0>", x).lower()
   6  
   7  def mc2us(x): # mixed case to underscore notation
   8      return cw2us(x)
   9  
  10  def us2mc(x): # underscore to mixed case notation
  11      return re.sub(r'_([a-z])', lambda m: (m.group(1).upper()), x)
  12  
  13  def us2cw(x): # underscore to capwords notation
  14      s = us2mc(x)
  15      return s[0].upper()+s[1:]

Result
   1  
   2  >>> cw2us("PrintHTML")
   3  'print_html'
   4  >>> cw2us("IOError")
   5  'io_error'
   6  >>> cw2us("SetXYPosition")
   7  'set_xy_position'
   8  >>> cw2us("GetX")
   9  'get_x'

Base 2 Conversion

Converting from base 2 to int is easy
   1  
   2  >>> int('1010', 2)
   3  10

The opposite is a bit involved.
   1  
   2  >>> number = 1000
   3  >>> hex2bin = {"0":"0000", "1":"0001", "2":"0010", "3":"0011",
   4              "4":"0100", "5":"0101", "6":"0110", "7":"0111",
   5              "8":"1000", "9":"1001", "A":"1010", "B":"1011",
   6              "C":"1100", "D":"1101", "E":"1110", "F":"1111"}
   7  >>> "".join([hex2bin[h] for h in '%X'%number]).lstrip('0')
   8  '1111101000'

See the def of toBase2(number) here

Converting between 2 google map tile types

Google maps is a marvelous app.
I try to program a prototype of it on pys60
http://bigbold.com/snippets/posts/show/458

There I only show the default map, not the satellite images.
Retrieving a different mode isn't that difficult.
I read the info from here
http://intepid.com/2005-07-17/21.50/
Then I begin comparing the 2 tile types of the same area
(around California)
http://mt.google.com/mt?v=w2.5&x=20&y=49&zoom=10 (map)
http://kh.google.com/kh?v=3&t=tqtsqrqt (satellite)

Here's the conversion routine between x,y,zoom and quadtree
   1  
   2  def quadtree(x,y, zoom):
   3  	out = []
   4  	m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
   5  	for i in range(17-zoom):
   6  		x, rx = divmod(x, 2)
   7  		y, ry = divmod(y, 2)
   8  		out.insert(0, m[(rx,ry)])
   9  	return 't' + ''.join(out)

Then to convert back
   1  
   2  def xyzoom(quad):
   3  	x, y, z = 0, 0, 17
   4  	m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)}
   5   	for c in quad[1:]:
   6  		x = x*2 + m[c][0]
   7  		y = y*2 + m[c][1]
   8  		z -= 1
   9  	return x, y, z

Using them is simple
   1  
   2  >>> quadtree(20,49,10)
   3  'tqtsqrqt'
   4  >>> xyzoom('tqtsqrqt')
   5  (20, 49, 10)
   6  >>> sat_url = 'http://kh.google.com/kh?v=3&t=' + quadtree(20,49,10)

Mass conversion from word to HTML

I got 1000 word files. Each contains 1 main image and some decorations.
(This is actually a big book scanned into 1-file-per-page format)
I need to extract all the images. What do I do?

Python can do some automation using COM. (or something like that)
   1  
   2  import pythoncom, win32com.client
   3  
   4  app = win32com.client.gencache.EnsureDispatch("Word.Application")
   5  
   6  doc = 'C:\\lang\\try\\bdham\\p1'
   7  app.Documents.Open(doc + '.doc')
   8  app.ActiveDocument.SaveAs(doc + '.html', FileFormat=win32com.client.constants.wdFormatHTML)
   9  app.ActiveDocument.Close()
  10  # now repeat with p2, p3, etc.

Actually, I should put it in a loop. But this non-loop version
is easier to read and remember.
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS