Skip to content

micropython support for Sound - #632

Merged
dwalton76 merged 6 commits into
ev3dev:ev3dev-stretchfrom
dwalton76:micropython-sound
May 19, 2019
Merged

dwalton76 merged 6 commits into
ev3dev:ev3dev-stretchfrom
dwalton76:micropython-sound

Conversation

@dwalton76

Copy link
Copy Markdown
Collaborator

For #628

Tested micropython and python3 via

from ev3dev2.sound import Sound

VOLUME = 30

sound = Sound()
sound.speak("hello world", volume=VOLUME, play_type=Sound.PLAY_WAIT_FOR_COMPLETE)

sound.play_song((
    ('Db4', 'e3'),
))

# Star Wars via play_song()
sound.play_song((
    ('D4', 'e3'),      # intro anacrouse
    ('D4', 'e3'),
    ('D4', 'e3'),
    ('G4', 'h'),       # meas 1
    ('D5', 'h'),
    ('C5', 'e3'),      # meas 2
    ('B4', 'e3'),
    ('A4', 'e3'),
    ('G5', 'h'),
    ('D5', 'q'),
    ('C5', 'e3'),      # meas 3
    ('B4', 'e3'),
    ('A4', 'e3'),
    ('G5', 'h'),
    ('D5', 'q'),
    ('C5', 'e3'),      # meas 4
    ('B4', 'e3'),
    ('C5', 'e3'),
    ('A4', 'h.'),
))

# Emperial March via tone()
sound.tone([
    (392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
    (466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
    (392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
    (587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
    (369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
    (784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
    (739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
    (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
    (523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
    (466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
    (311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
    (466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
    (392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
    (659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
    (554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
    (466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
    (392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
    (392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
])

sound.play_file("/usr/share/sounds/ev3dev/mechanical/airbrake.wav", volume=VOLUME, play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)

@dwalton76 dwalton76 changed the title Micropython sound micropython support for Sound May 13, 2019
@WasabiFan

Copy link
Copy Markdown
Member

This changes the API, right? It removes one of the "play type" options and removes the handle returns?

@dwalton76

Copy link
Copy Markdown
Collaborator Author

All of the play_type options are still the same. The return value has changed if you are doing play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE...that used to return the PID of the subprocess but now we just return None. Our default is Sound.PLAY_WAIT_FOR_COMPLETE...my gut says there probably aren't a ton of folks using the NO_WAIT option and doing much with that returned PID. With the proposed change they could always figure out the PID by looking at the processes that are currently running.

@WasabiFan

Copy link
Copy Markdown
Member

Ok, so am I correct in thinking that with this change there isn't an API to play a sound asynchronously (i.e., start it then keep going while it plays)? I feel like that behavior could be useful, as it might be nice to speak/beep some status while your robot is still going.

@dwalton76

Copy link
Copy Markdown
Collaborator Author

No, that functionality is still there, to do that you use play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE.

@WasabiFan

Copy link
Copy Markdown
Member

I'm still a bit concerned about the breaking change on the return type. Perhaps this is unfounded, but it feels like this might cause problems for existing consumers who do something more advanced.

I think we could instead split the Popen stuff into its own method (i.e., just have it as a generic "run audio command" method) and then have it fall back to os.system when needed. That would probably be less of a code change, would preserve the existing behavior, and introduce support for the additional platform.

I think this would probably work:

def _audio_command(self, command, play_type=PLAY_WAIT_FOR_COMPLETE):
    if is_micropython():
        if play_type == Sound.PLAY_WAIT_FOR_COMPLETE:
            os.system(command)
        else:
            os.system('{} &'.format(command))
        
        return None
    else:
        with open(os.devnull, 'w') as n:
            subprocess = Popen(shlex.split(command), stdout=n)
            if play_type == Sound.PLAY_WAIT_FOR_COMPLETE:
                subprocess.wait()
                return None
            else:
                return subprocess

It should be fairly straightforward to rewrite the other functions in terms of this, and that would get all the behavior we need.

@dwalton76

Copy link
Copy Markdown
Collaborator Author

I think this is backwards compatible now. The only quirk is that the return behavior documented in the docstrings is incorrect for micropython...it will not return the PID of the subprocess. Do we want to document python3 vs micropython behavior? I don't know that we do that elsewhere.

@WasabiFan WasabiFan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This seems reasonable to me; I think it would make the most sense to add a note to those doc comments saying that it doesn't return the object on Micropython.

Comment thread ev3dev2/sound.py Resolved
Comment thread ev3dev2/sound.py Resolved
@dwalton76

Copy link
Copy Markdown
Collaborator Author

Will update the docstrings to mention about the return values when using python3 vs micropython and then merge this one.

@dwalton76
dwalton76 merged commit bae544f into ev3dev:ev3dev-stretch May 19, 2019
@dwalton76
dwalton76 deleted the micropython-sound branch May 19, 2019 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants