meta data for this page
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
maemo:python_and_n900 [2011/08/09 10:37] japskua [Using Location API] |
maemo:python_and_n900 [2011/08/11 09:56] (current) japskua [Using Microphone] |
||
---|---|---|---|
Line 12: | Line 12: | ||
It is also possible to access C APIs that don't have Python bindings. However, this can bring some interesting problems. Not recommended, | It is also possible to access C APIs that don't have Python bindings. However, this can bring some interesting problems. Not recommended, | ||
+ | |||
+ | ===== Playing Sounds ===== | ||
+ | |||
+ | N900 can play sounds with two different ways - Either using gstreamer or pygame. This example is made with pygame as it provides a higher-level API and requires less magic words ;-) Pygame documentation can be found behind the following link: [[http:// | ||
+ | |||
+ | The sound file used in this example: {{: | ||
+ | |||
+ | Here is a proper Object-Oriented implementation of a Player that can load and play sound files. I suggest creating a list of sound objects that are loaded with this player and then playing those when needed (faster than opening a file for playback each time separately). | ||
+ | |||
+ | <file python soundplayer.py> | ||
+ | """ | ||
+ | August 9, 2011 | ||
+ | |||
+ | @author: Janne Parkkila | ||
+ | @email: japskua@gmail.com | ||
+ | |||
+ | @summmary: This file contains code for playing music/ | ||
+ | with Python programming language. This implementation uses | ||
+ | | ||
+ | """ | ||
+ | |||
+ | import pygame | ||
+ | |||
+ | class Player(object): | ||
+ | |||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: The Constructor class | ||
+ | """ | ||
+ | pygame.mixer.quit() | ||
+ | pygame.mixer.init() | ||
+ | pygame.init() | ||
+ | |||
+ | def Play(self, sound): | ||
+ | """ | ||
+ | @summary: This function is used to play the given sound | ||
+ | @param sound: The sound to be played | ||
+ | @type sound: pygame sound object | ||
+ | """ | ||
+ | sound.play() | ||
+ | |||
+ | def Load(self, filename): | ||
+ | """ | ||
+ | @summary: This function loads the given filename into a pygame | ||
+ | readable sound object | ||
+ | @param filename: The path to the file to be loaded | ||
+ | @type filename: String | ||
+ | @return: Loaded pygame music object | ||
+ | @rtype: pygame Sound | ||
+ | """ | ||
+ | | ||
+ | return pygame.mixer.Sound(filename) | ||
+ | |||
+ | if __name__ == " | ||
+ | # Initialize the player | ||
+ | player = Player() | ||
+ | # Load the soundfile with the player | ||
+ | sound = player.Load(" | ||
+ | # Play the file | ||
+ | player.Play(sound) | ||
+ | </ | ||
+ | |||
+ | However, if you want to make something better yourself, here is the basic simple version of the same thing | ||
+ | |||
+ | <file python simple-sounds.py> | ||
+ | import pygame | ||
+ | |||
+ | pygame.mixer.init() | ||
+ | pygame.init() | ||
+ | |||
+ | sound = pygame.mixer.Sound(" | ||
+ | sound.play() | ||
+ | </ | ||
+ | ===== Using Microphone ===== | ||
+ | |||
+ | This code snippet uses gstreamer to record voice through the phone microphone. Notice that there is a small lag on starting the recording and after stopping the record. This is purely related to gstreamer and should be taken into account when developing applications. So, don't wait zero response time with this code :-P | ||
+ | |||
+ | **Note: If this doesn' | ||
+ | Get them with: | ||
+ | <code shell> | ||
+ | sudo gainroot | ||
+ | apt-get install gstreamer0.10-alsa gstreamer0.10-flac gstreamer0.10-plugins-good-extra | ||
+ | </ | ||
+ | |||
+ | |||
+ | <file python microphone.py> | ||
+ | # | ||
+ | |||
+ | # This is the MIT license: | ||
+ | # http:// | ||
+ | |||
+ | # Copyright (c) 2009 Digital Achievement Incorporated and contributors. | ||
+ | |||
+ | # Permission is hereby granted, free of charge, to any person obtaining | ||
+ | # a copy of this software and associated documentation files (the | ||
+ | # " | ||
+ | # without limitation the rights to use, copy, modify, merge, publish, | ||
+ | # distribute, sublicense, and/or sell copies of the Software, and to | ||
+ | # permit persons to whom the Software is furnished to do so, subject to | ||
+ | # the following conditions: | ||
+ | |||
+ | # The above copyright notice and this permission notice shall be | ||
+ | # included in all copies or substantial portions of the Software. | ||
+ | |||
+ | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
+ | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
+ | # MERCHANTABILITY, | ||
+ | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
+ | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
+ | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
+ | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
+ | # | ||
+ | |||
+ | """ | ||
+ | @summary: This file contains the data for SoundRecorder on Nokia N900 using Python and gstreamer. | ||
+ | The code is based on the source by Digital Achievement and is available from: | ||
+ | http:// | ||
+ | @author: Janne Parkkila (the modified Object Oriented Version) | ||
+ | @email: japskua@gmail.com | ||
+ | |||
+ | @requires: gstreamer0.10-alsa, | ||
+ | """ | ||
+ | |||
+ | from optparse import OptionParser | ||
+ | |||
+ | import dbus | ||
+ | import gst | ||
+ | import sys | ||
+ | import time | ||
+ | |||
+ | class SoundRecorder(object): | ||
+ | """ | ||
+ | @summary: This is the SoundRecorder class that handles locating the microphone | ||
+ | and capturing voice from the mic. Currently uses somewhat " | ||
+ | calling gstreamer via command line, but works pretty well nonetheless :-D | ||
+ | """ | ||
+ | |||
+ | mic = None | ||
+ | |||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: The Constructor class, that searches for the microphone on initialization | ||
+ | """ | ||
+ | bus = dbus.SystemBus() | ||
+ | hal_manager = bus.get_object(" | ||
+ | "/ | ||
+ | hal_manager = dbus.Interface(hal_manager, | ||
+ | | ||
+ | print hal_manager | ||
+ | devices = hal_manager.FindDeviceStringMatch(" | ||
+ | | ||
+ | identifiers = [] | ||
+ | |||
+ | for dev in devices: | ||
+ | device = bus.get_object(" | ||
+ | | ||
+ | card = device.GetAllProperties(dbus_interface=" | ||
+ | if card[" | ||
+ | print "%d. %s" % (card[" | ||
+ | identifiers.append(card[" | ||
+ | |||
+ | self.mic = identifiers[0] | ||
+ | | ||
+ | | ||
+ | def Record(self, | ||
+ | """ | ||
+ | @summary: Records the audio with the microphone and saves to the given destination | ||
+ | @param destination: | ||
+ | @type destination: | ||
+ | """ | ||
+ | | ||
+ | pipeline = gst.parse_launch(""" | ||
+ | pipeline.set_state(gst.STATE_PLAYING) | ||
+ | | ||
+ | print " | ||
+ | sys.stdin.readline() | ||
+ | | ||
+ | pipeline.set_state(gst.STATE_NULL) | ||
+ | time.sleep(5) | ||
+ | |||
+ | if __name__ == " | ||
+ | # This is used if the code is run straight from the command line | ||
+ | recorder = SoundRecorder() | ||
+ | recorder.Record(" | ||
+ | </ | ||
+ | ===== Using Camera ===== | ||
+ | |||
+ | This code uses the N900's camera to take picture. It is not very sophisticated, | ||
+ | |||
+ | so please, before running any camera python apps (at least based on my code) make sure you do | ||
+ | |||
+ | <code shell> | ||
+ | sudo gainroot | ||
+ | python camera.py | ||
+ | |||
+ | (or in scratchbox) | ||
+ | |||
+ | fakeroot | ||
+ | python2.5 camera.py | ||
+ | </ | ||
+ | |||
+ | <file python camera.py> | ||
+ | """ | ||
+ | August 9, 2011 | ||
+ | |||
+ | @author: Janne Parkkila | ||
+ | @email: japskua@gmail.com | ||
+ | |||
+ | @summary: This file contains code for taking pictures with the front or the back | ||
+ | camera of the nokia N900. The file provides python access to the camera | ||
+ | but really just uses command line to take picture. | ||
+ | |||
+ | @note: PyMaemo has no possiblity to capture pictures, thus this gstreamer capture is used | ||
+ | via command line. This requires super user rights, so if you are using this file | ||
+ | you can only take pictures with as a sudoer. | ||
+ | |||
+ | ALSO! MAKE SURE THE CAMERA LID IS OPEN WHEN TAKING THE PICTURE!!! :-P | ||
+ | """ | ||
+ | import sys | ||
+ | import gst | ||
+ | import os | ||
+ | |||
+ | |||
+ | class Camera(object): | ||
+ | """ | ||
+ | @summmary: This is the camera object that is capable of capturing pictures | ||
+ | with either the front or the back camera of the device | ||
+ | """ | ||
+ | back = None | ||
+ | front = None | ||
+ | |||
+ | def __init__(self): | ||
+ | """ | ||
+ | @summary: The Constructor | ||
+ | """ | ||
+ | self.back = " | ||
+ | self.front = " | ||
+ | |||
+ | def TakePicture(self, | ||
+ | """ | ||
+ | @summary: The function for taking pictures with the camera | ||
+ | @param camera: The camera to be taken pictures with | ||
+ | @type camera: String, either " | ||
+ | @param filename: The path to the file where to save the taken picture, | ||
+ | | ||
+ | @type filename: String | ||
+ | """ | ||
+ | photo = None | ||
+ | |||
+ | if camera == " | ||
+ | photo = self.front + filename + " | ||
+ | elif camera == " | ||
+ | photo = self.back + filename + " | ||
+ | else: | ||
+ | print " | ||
+ | | ||
+ | os.system(photo) | ||
+ | |||
+ | |||
+ | |||
+ | # If this program is executed as itself | ||
+ | if __name__ == " | ||
+ | camera = Camera() | ||
+ | camera.TakePicture(" | ||
+ | camera.TakePicture(" | ||
+ | </ | ||
+ | ===== Bluetooth ===== | ||
+ | |||
+ | Bluetooth stuff here | ||
===== Using Accelerometer ===== | ===== Using Accelerometer ===== | ||
Line 85: | Line 354: | ||
**IMPORTANT TO NOTICE** The GPS on the N900 is not very fast! Acquiring a signal can take a great amount of time. Thus it is just recommended to simulate this in some other manner. The probability of receiving a signal in the basement is really small, so please, don't waste too much of your precious time with this. | **IMPORTANT TO NOTICE** The GPS on the N900 is not very fast! Acquiring a signal can take a great amount of time. Thus it is just recommended to simulate this in some other manner. The probability of receiving a signal in the basement is really small, so please, don't waste too much of your precious time with this. | ||
- | More info on location API with pyMaemo can be found from [[http:// | + | More info on location API with pyMaemo can be found from: |
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
The following file is brutally ripped from the Maemo wiki: | The following file is brutally ripped from the Maemo wiki: |