Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 51
Текст из файла (страница 51)
Receiving the same string again increases thefrequency. Receiving the string ‘52’ decreases the frequency. When thepatch starts up there is no sound to be heard, until the sound is switchedon. This is done by sending the string ‘53’ from the phone. To switch offthe sound, the string ‘53’ must be sent by the phone again.In this case we are not using the built-in UI elements of PyS60, butinstead we build our own graphical user interface (Figure 11.9) made upof JPEG images.
We have a button for switching the sound on and off anda slider to change the frequency of the generated sound on the Max/MSPapplication. This interface uses six images (Figure 11.10).Figure 11.9Graphical slider and switch for sound controlThe code is divided into two parts (Examples 121 and 122). They needto be combined into one script to work. The first 20 lines of code areused to turn the graphical elements into ready-to-use image objects (seeChapter 5).Whenever the screen must be redrawn (for example, when the positionof the slider changes), we call the function handle redraw() in whichwe use the concept of double buffering by copying all the graphicalelements e.g.
img.blit(slidershaft, target = (0,0,w,h)) andimg.blit(contr,target=(142,y pos contr),mask=contrMask) to the img object. We then blit the img object to thecanvas:canvas.blit(img, target = (0,0,w,h), scale = 1 )CONTROLLING MAX/MSP WITH A PHONE269(b)(c)(d)(e)(a)(f)Figure 11.10 The images for slider and switch: (a) background, background.jpg,(b) slider, controller.jpg, (c) 1-bit mask for the slider, controller mask.png,(d) switch on, button red.jpg, (e) switch off, button dark.jpg, (f) 1-bit mask for theswitch, button mask.pngTo connect to Max/MSP over Bluetooth, we use the function connect() (Example 122), which includes the same code as in previousBluetooth RFCOMM examples, basically setting up a socket and scanningfor Bluetooth devices. For programming keyboard keys, we use the codethat was described in detail in Section 5.2.2.Example 121: Max/MSP using Bluetoothimport appuifw, e32, graphics, key_codes, socketsound = 0y_pos_contr = 100slidershaft = graphics.Image.open("e:\\background.jpg")makeMaskTemp = graphics.Image.open('e:\\controller_mask.jpg')makeMaskTemp.save("e:\\controller_mask.png", bpp=1)contrMask = graphics.Image.new(size = (97,149),mode = '1')contrMask.load("e:\\controller_mask.png")contr = graphics.Image.open("e:\\controller.jpg")makeMaskTemp = graphics.Image.open('e:\\button_mask.jpg')makeMaskTemp.save("e:\\button_mask.png", bpp=1)buttnMask = graphics.Image.new(size = (111,78),mode = '1')buttnMask.load("e:\\button_mask.png")buttnOn = graphics.Image.open("e:\\button_red.jpg")270COMBINING ART AND ENGINEERINGbuttnOff = graphics.Image.open("e:\\button_dark.jpg")def keys(event):global y_pos_contr, soundif event['keycode'] == key_codes.EKeyDownArrow:if y_pos_contr < 260 :y_pos_contr = y_pos_contr + 5sending(str(3))if event['keycode'] == key_codes.EKeyUpArrow:if y_pos_contr > 0 :y_pos_contr = y_pos_contr – 5sending(str(4))if event['keycode'] == key_codes.EKeySelect:if sound == 1:sound = 0else:sound = 1sending(str(5))handle_redraw(())The ‘Select’ key is used to switch the sound on and off.
Each timethe ‘Select’ key is pressed we send the string ‘5’ in ASCII format withsending(str(5)) to Max/MSP. ‘5’ is equal to 53 in decimal formatand ‘53’ is used in the Max/MSP patch.The ‘ArrowUp’ key is used to increase the frequency of the soundgenerated by the Max/MSP application. When that key is pressed we sendthe string ‘3’ in ASCII with sending(str(3)) which is equal to ‘51’ indecimal format used by Max/MSP.
Also the y-position of the controllerimage is changed by 5 pixels with y pos contr = y pos contr – 5,making it blit to the canvas slightly further up. The same logic is usedfor decreasing the frequency of the generated sound at the Max/MSPapplication with the ‘ArrowDown’ key instead (sending the string ‘4’ inASCII (‘52’ in decimal) as well as changing the y-position of the controllerimage downwards on the screen).Example 122: Max/MSP using Bluetooth (2/2)def handle_redraw(rect):global sound, img, w,himg.blit(slidershaft, target = (0,0,w,h))img.blit(contr, target=(142,y_pos_contr), mask=contrMask)if sound == 1:img.blit(buttnOn, target=(8,328), mask=buttnMask)else:img.blit(buttnOff, target=(8,328), mask=buttnMask)canvas.blit(img, target = (0,0,w,h), scale = 1 )def choose_service(services):names = []CONTROLLING MAX/MSP WITH A PHONE271channels = []for name, channel in services.items():names.append(name)channels.append(channel)index = appuifw.popup_menu(names, u"Choose service")return channels[index]def connect():global sockaddress, services = socket.bt_discover()channel = choose_service(services)sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM)sock.connect((address, channel))def sending(data):global socksock.send(data)def quit():app_lock.signal()canvas=appuifw.Canvas(event_callback=keys, redraw_callback=handle_redraw)appuifw.app.body=canvasappuifw.app.screen='full'w, h = canvas.sizeimg = graphics.Image.new((w,h))appuifw.app.exit_key_handler=quithandle_redraw(())connect()app_lock = e32.Ao_lock()app_lock.wait()11.5.2 Connecting a Phone to Max/MSP using WiFiWhen connecting a phone to Max/MSP using WiFi, we can use almostthe same files as described in Section 11.5.1 about using Bluetooth.
Afew things are different and we explain the differences here.We need to remove the connect() function and replace the code ofthe sending() function with the code in Example 123.Example 123: Max/MSP using TCP/IPdef sending(data):HOST = '192.168.1.100' # The remote hostPORT = 9000 # The same port as used by the servers = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT))s.send(data)print "data send:", datas.close()We create a TCP/IP socket with s = socket.socket(socket.AFINET, socket.SOCK STREAM) and use it to connect with s.connect272COMBINING ART AND ENGINEERING((HOST, PORT)) to an IP socket on our computer identified by the IPaddress (HOST). We choose port 9000 for communication. Sending thecontrol strings ‘51’, ‘52’ and ‘53’ is done through s.send(data).In Figure 11.11, we can see the patch for Max/MSP.
There is onlyone major difference between it and the Bluetooth diagram (Figure 11.8).Figure 11.11 Max/MSP patch using TCP portOPENSOUND CONTROL273The TCP version uses the mx j.net.tcp.rec@port 9000 object toreceive the control strings, instead of the serial a 9600 object. Themx j.net.tcp.rec@port 9000 object handles socket communicationover TCP in Max/MSP and is one of the simplest ways to handle HTTPtransfers.Once the Max/MSP application is running, the computer is ready tosend and receive data over the socket. It is important that the communication is done through port 9000 using the current IP address of yourcomputer.
This means the port must be open and firewall settings mustbe set accordingly.Environment B in Chapter 8 describes how to use your local computerto work as a test server which is accessed from Internet. But the scenariodescribed here does not directly use Environment B. Instead, we wantto set up a local WiFi network using a wireless broadband router towhich we directly connect the computer running Max/MSP, as well asconnecting all the mobile devices.This scenario has many advantages. For example, you can run mobilemulti-user applications over WiFi in any place without having to havea connection to the Internet, simply using your computer as a server forsocket communication, to which the mobile devices connect.
Further,this setup has the advantage that the wireless broadband router can bemanually configured to assign an IP address to the computer. Thereforewe know the IP address of our computer inside the local WiFi networkand can use it to hardcode the destination address for ‘server’ socket communication which we need to specify in our PyS60 code (to successfullyconnect the mobile devices to the computer).In the given scenario, each connected mobile device can switch thesound on and off as well as changing the frequency of the sound. Thismeans that it can become messy when many users want to control onesound at the same time. There are many solutions to make the applicationmore interesting and useful, such as having a controllable sound sourcewith multiple parameters available for controlling different aspects of theone sound.
Each mobile user can then control different parameters. Butwe want to leave it up to the readers of this book to expand the examples;this example just explains some basic principles.Section 11.6 explains an important protocol for handling multi-userissues in the field of digital music: OpenSoundControl OSC.11.6 OpenSound ControlOpenSound Control (OSC, www.cnmat.berkeley.edu/OpenSoundControl) is a protocol for communication among computers, soundsynthesizers and other multimedia devices. It is optimized for modernnetworking technology. Often it is used for multi-user applications and274COMBINING ART AND ENGINEERINGit is frequently deployed by the arts and design community. Interest inusing mobile phones as part of multi-modal interfaces is on the rise.We can use OSC with PyS60 by installing an additional Python librarycalled OSCmobile.py to our phone into a folder named python/Libon our memory card (if the folder doesn’t exist, you can create it).
You candownload the library from www.mobilenin.com/pys60/oscmobile.htm.This library is based on the OpenSound Control library implementation for Python by Daniel Holth and Clinton McChesney, found athttp://wiretap.stetson.edu.In this example, we can use the Bluetooth code from Examples 121and 122. All we need to change is to import the module OSCmobileand replace the code of the function sending() with the code inExample 124.Example 124: OSC for mobile phonesimport OSCmobiledef sending(data):global sockmessage = OSCmobile.OSCMessage()message.setAddress("/phone/user1")message.append(data)sock.send(message.getBinary())This allows you to run exactly the same application setup as inExamples 121 and 122, except that you are now using OSC to encodemessages before sending them to Max/MSP on the computer over Bluetooth.In a multi-user scenario, different phones can use different names, forexample user2 and user3, inside the address string of the OSC messageto distinguish by which phone the message has been sent and to address,for instance, individual sound parameters.Please check Chapter 7 to see how to receive data from a computerover Bluetooth RFCOMM.