Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: My first python program - A simple Metronome

  1. #1
    Join Date
    May 2008
    Location
    Porto / Portugal
    Beans
    94
    Distro
    Ubuntu Studio 10.04 Lucid Lynx

    Smile My first python program - A simple Metronome

    Well here I left the code of a simple metronome that I put together, made with the help of people in this forum (thanks strider1551 for the delay routine ) who guide me to the choice of python.

    well enough said I leave here the code, its very crude and lacks a better user interface, for now I want to make it work . I have a problem, I only can stop the program with ctrl+c interrupt, if anyone can help and give some direction, it will be nice, (maybe using the letter "q" for quit.

    Thanks everyone, it is a happy day

    PHP Code:
    #!usr/bin/python
    # This is a simple metronome coded in Python, my first experience with python
    # Any ideas, corrections and comments pleas email-me 
    # ricardolameiro at y a h oo dot com (sorry, anti spam here)
    #
    #
    # needs Python module  tkSnack  developed at KTH in Stockholm, Sweden
    # free download: snack229-py.zip
    # from: http://www.speech.kth.se/snack/
    #
    # This code is free, for any type of use, except commercial
    # I assume I kind of GPL

    # (c) Ricardo Lameiro



    import Tkinter
    import tkSnack
    import time

     
    def playNote
    (freqduration):
        
    """play a note of freq (hertz) for duration (seconds)"""
        
    snd tkSnack.Sound()
        
    filt tkSnack.Filter('generator'freq300000.0'sine'int(11500*duration))
        
    snd.stop()
        
    snd.play(filter=filtblocking=1)
     
    def soundStop():
        
    """stop the sound the hard way"""
        
    try:
            
    root root.destroy()
            
    filt None
        except
    :
            
    pass
    #insert Beat per minute routine
    ubpm raw_input('Insert BPM please: ')
    #this transforms the bpm time, into a usable time delay var
    bpm 60.0 int(ubpm
    #this subtracts the time used by the sound beep, so the total delays is
    # BPM = beeptime + delay
    delay bpm 0.1

    root 
    Tkinter.Tk()
    tkSnack.initializeSnack(root)

    tkSnack.audio.play_gain(80)
    #delay routine
    while True:
        
    playNote(8800.1)
        
    soundStop()
        
    time.sleep(delay)




    root.withdraw() 

    every comment is welcomed
    I tried to comment the less explicit things

  2. #2
    Join Date
    Feb 2007
    Location
    Edinburgh, Scotland
    Beans
    391

    Re: My first python program - A simple Metronome

    Nice work for your first program. Got a couple of comments:

    1 - The code/layout is quite messy, there is quite a lot of erratic whitespace/lack of whitespace around. Trust me, you will thank yourself in the future if you keep your code neat and tidy.

    2 - The program crashes if you tell it to go over 600bpm. It might be an idea to limit that to prevent crashes

    3 - It would be really cool if it took arguments from the command line. Instead of doing
    Code:
    ubpm = raw_input('Insert BPM please: ')
    you could do
    Code:
    ubpm = sys.argv[0]
    which would let you run the program as "./metronome.py 250". You would also need to import the sys module for this to work.

    Other than that good job! Cant see any other problems with it.
    Today we have 15 minutes of fame, tomorrow 15 minutes of anonymity.
    My Blog | Veza - Opensource TinyURL clone with sensible URLs

  3. #3
    Join Date
    May 2008
    Location
    Porto / Portugal
    Beans
    94
    Distro
    Ubuntu Studio 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    Ok. After the ideas of bobbocanfly, I made some alterations to the code. About the looks of the code, I am a newbie starting now, don't have a teacher, just tutorials, e-books and one book, some help in this department is appreciated.

    I have still one doubt, How can I know if the timing(delay) its going correctly? I have eared of threads, but don't know what it is, any thougs in this is also appreciated.

    And please say your opinion, even if it is hard

    here is the code:


    PHP Code:
    #!usr/bin/python
    # This is a simple metronome coded in Python, my first experience with python
    # Any ideas, corrections and comments pleas email-me 
    # ricardolameiro at y a h oo dot com (sorry, anti spam here)
    #
    #
    # needs Python module  tkSnack  developed at KTH in Stockholm, Sweden
    # free download: snack229-py.zip
    # from: http://www.speech.kth.se/snack/
    #
    # This code is free, for any type of use, except commercial
    # I assume I kind of GPL

    # (c) Ricardo Lameiro



    import Tkinter
    import tkSnack
    import time
    import sys

     
    def playNote
    (freqduration):
        
    """play a note of freq (hertz) for duration (seconds)"""
        
    snd tkSnack.Sound()
        
    filt tkSnack.Filter('generator'freq300000.0'sine'int(11500*duration))
        
    snd.stop()
        
    snd.play(filter=filtblocking=1)


     
    def soundStop():
        
    """stop the sound the hard way"""
        
    try:
            
    root root.destroy()
            
    filt None
        except
    :
            
    pass



    #insert Beat per minute routine
    #comment if you whant to use raw inmput

    ubpm sys.argv[1]

    #uncomment if want to use raw_imput

    #ubpm = raw_input('Insert BPM please: ')

    #protects for errors, maximum bpm is 300

    Ubpm int(ubpm)
    if 
    Ubpm 300:
        
    BPM 300

    else :
        
    BPM ubpm


        
    #this transforms the bpm time, into a usable time delay var

    bpm 60.0 int(BPM)

    #this subtracts the time used by the sound beep, so the total delays is
    # BPM = beeptime + delay

    print bpm
    delay 
    bpm 0.1



    root 
    Tkinter.Tk()
    tkSnack.initializeSnack(root)

    tkSnack.audio.play_gain(80)



    #delay routine
    while True:
        
    playNote(8800.1)
        
    soundStop()
        
    time.sleep(delay)




    root.withdraw() 
    thanks for looking

  4. #4
    Join Date
    May 2008
    Location
    jordan
    Beans
    311
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: My first python program - A simple Metronome

    Execuse me , But I need how to Compile The Files

    Of Python & C in Ubuntu hardy

  5. #5
    Join Date
    May 2008
    Location
    Porto / Portugal
    Beans
    94
    Distro
    Ubuntu Studio 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    Uchiha_madara I am sorry, but if you refer to my code, there is no C in it. About the rest, python is interpreted language, just type $python nameofprogram.py and it will run. About c I really dont know that, but I am sure that is plenti of tutorials out there to do that.

  6. #6
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    Quote Originally Posted by Uchiha_madara View Post
    Execuse me , But I need how to Compile The Files

    Of Python & C in Ubuntu hardy
    python is "interpreted" (but it also compiles, automatically). Anyway, to run a python script on ubuntu type on the shell:

    Code:
    python myfolder/myscript.py
    And for C and C++ you need build-essential, see the tutorial
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  7. #7
    Join Date
    Jan 2007
    Location
    Germany, Kiel
    Beans
    1,093
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: My first python program - A simple Metronome

    You should use:
    #!usr/bin/env python
    instead of:
    #!usr/bin/python
    It's more compatible.
    Also you could instead of uncommenting the raw_input line just add an if-statement that checks if ubpm is null. That way, you can use raw_input if no arguments are given or use the arguments if given.
    I just love it when you're being sarcastic --aks44

  8. #8
    Join Date
    May 2008
    Location
    Porto / Portugal
    Beans
    94
    Distro
    Ubuntu Studio 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    OK, here is the code with the modifications.

    I have a little question, the line
    Code:
    #!usr/bin/env python
    is suposed to run the script directly from the shell correct?
    Code:
    ./metronome.py
    I do this but is not working even with the prior path, I am in ubuntu 8.04 some help here please.


    PHP Code:
    #!usr/bin/env python
    # This is a simple metronome coded in Python, my first experience with python
    # Any ideas, corrections and comments pleas email-me 
    # ricardolameiro at y a h oo dot com (sorry, anti spam here)
    #
    #
    # needs Python module  tkSnack  developed at KTH in Stockholm, Sweden
    # free download: snack229-py.zip
    # from: http://www.speech.kth.se/snack/
    #
    # This code is free, for any type of use, except commercial
    # I assume a kind of GPL

    # copyright (c) Ricardo Lameiro 2008



    import Tkinter
    import tkSnack
    import time
    import sys

     
    def playNote
    (freqduration):
        
    """play a note of freq (hertz) for duration (seconds)"""
        
    snd tkSnack.Sound()
        
    filt tkSnack.Filter('generator'freq300000.0'sine'int(11500*duration))
        
    snd.stop()
        
    snd.play(filter=filtblocking=1)


     
    def soundStop():
        
    """stop the sound the hard way"""
        
    try:
            
    root root.destroy()
            
    filt None
        except
    :
            
    pass



    #insert Beat per minute routine

    if len(sys.argv) == 1:
        
    ubpm raw_input('Insert BPM please: ')
    else:
        
    ubpm sys.argv[1]



    #protects for errors, maximum bpm is 300

    Ubpm int(ubpm)
    if 
    Ubpm 300:
        
    BPM 300
    else :
        
    BPM ubpm



    if BPM == 300:
        print 
    "The maximum BPM is 300, using 300 BPM"
    else:
        
    pass
        


    #this transforms the bpm time, into a usable time delay var

    bpm 60.0 int(BPM)



    #this subtracts the time used by the sound beep, so the total delays is
    # BPM = beeptime + delay

    delay bpm 0.1



    root 
    Tkinter.Tk()
    tkSnack.initializeSnack(root)

    tkSnack.audio.play_gain(80)



    #delay routine

    while True:
        
    playNote(8800.1)
        
    soundStop()
        
    time.sleep(delay)



    root.withdraw() 
    Last edited by rlameiro; June 12th, 2008 at 12:53 AM. Reason: misspellings

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    #!/usr/bin/env python

    And you should still run your application with "python myprogram.py", that way python can remake the .pyc files.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    May 2008
    Location
    Porto / Portugal
    Beans
    94
    Distro
    Ubuntu Studio 10.04 Lucid Lynx

    Re: My first python program - A simple Metronome

    ok, I changed it, but is it possible to run the script just typing $./myprogram.py ???

    I tried it and didn't worked
    Last edited by rlameiro; June 12th, 2008 at 02:11 AM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •