GithubHelp home page GithubHelp logo

fizban99 / upy-rtttl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dhylands/upy-rtttl

0.0 2.0 0.0 7 KB

Python Parser for Ring Tone Text Transfer Language (RTTTL)

License: MIT License

Python 100.00%

upy-rtttl's Introduction

Ring Tone Text Transfer Language Parser

This library can parse RTTTL text lines and delivers the frequency and duration for each note.

A typical RTTTL string looks like this:

Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e,c6,8e,2c.6,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p,8d,8d#,8e,c6,8e,c6,8e,2c.6,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6

You can find many more sample ring tones here: http://www.picaxe.com/RTTTL-Ringtones-for-Tune-Command/

You can find a description of RTTTL here: https://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language

Using RTTTL on the pyboad

Instantiate an instance of the RTTTL class, passing in the tune string to the constructor.

tune = RTTTL('Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e,c6,8e,2c.6,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p,8d,8d#,8e,c6,8e,c6,8e,2c.6,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6')

Then use the notes generator to enumerate the notes in the tune. The notes generator will return a tuple, where the first entry in the tuple contains the frequency of the note (in Hz) and the second entry in the tuple contains the duration of the note.

for freq, msec in tune.notes()
    play_tone(freq, msec)

When using a piezo you basically provide a 50% PWM signal to the piezo using the frequency of the note. Some piezo speakers can vary the volume by using a different duty cycle. The piezo on the G30DEV board I tested this on, it didn't seem to make any difference.

In order to distinguish consequtive notes, you need a small gap between the notes. I chose to use 90% of the duration to play the tone, and 10% of duration to play silence.

I used the following play_tone routine on the G30DEV board:

import pyb
buz_tim = pyb.Timer(3, freq=440)
buz_ch = buz_tim.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.BUZZER, pulse_width=0)
def play_tone(freq, msec):
    print('freq = {:6.1f} msec = {:6.1f}'.format(freq, msec))
    if freq > 0:
        buz_tim.freq(freq)
        buz_ch.pulse_width_percent(50)
    pyb.delay(int(msec * 0.9))
    buz_ch.pulse_width_percent(0)
    pyb.delay(int(msec * 0.1))

I put a recording of the above on YouTube: https://youtu.be/TadV2AEvfww

The G30DEV board definition files for MicroPython can be found here: https://github.com/dhylands/G30DEV

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.