DestinysOtherChild
Posts: 2
Joined: Mon Jan 14, 2019 8:34 pm

Story Telling Machine

Mon Jan 14, 2019 9:57 pm

Hi new friends!

I'm apparently worse at Python than I thought I was. And I knew I was terrible. I am now desperate for help! My daughter has trouble falling asleep. We have been putting audiobooks on her tablet to help her fall asleep, but if the book runs out, she has to look at her tablet screen to start another one. And screens wake you up! So, it's a vicious cycle.

I found some instructions for a white noise machine that played a random MP3 of rain sounds. I was going to modify it a bit to work for my project. I bought a pi 0 w and a speaker pHAT. Soldered that bad boy together, and added a button to start the PI after shut down. Everything works great! So, I went in to build the code.

Turns out, the instructions I had were extremely out of date. And I can't confirm they worked even when they were cutting edge... I have looked at a bunch of other similar projects and haven't been able to figure it out at all. At this point, the only code I KNOW I have that works is,

Code: Select all


import random

print("Wow, I am terrible at this")

As I said, I am getting desperate. I planned to give my daughter her new storytelling machine for Christmas. But, that has come and gone. I have spent several nights since staring at my monitor, watching youtube videos, and reading how-tos, etc. It doesn't seem like a hard project, but I can't get past the first few lines of code without getting error messages.

I would like the code to play a random mp3 file from a folder (I am happy to type a list of all the files in the folder if that is easier than pulling a random file from a folder. If that makes sense.). Then shut the PI down (shut down can be after a specified time if that is easier than after a file is finished playing).

It's a PI 0 w with Raspian, which has been updated recently. I would like it to play the files on VLC Media Player, as I have the Speaker pHAT configured to work on VLC.

If anyone knows of or is working on, similar projects that are working in Python 3, please point me that direction! I haven't been able to find anything. If anyone has a spare few minutes to help a little girl and wants to write some code, that would be most appreciated too! (though, that is obviously a huge ask.)

Thanks in advance!

PS, here's the hardware.
pHAT Project.jpg
pHAT Project.jpg (159.97 KiB) Viewed 715 times

Andyroo
Posts: 3311
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Story Telling Machine

Mon Jan 14, 2019 10:13 pm

We all have to start somewhere - though tradition has ‘Hello World!’ as your first program :lol:

Have a read of https://stackoverflow.com/questions/292 ... -in-python and shout if (or when :oops: ) you get stuck.
Need Pi spray - these things are breeding in my house...

User avatar
Imperf3kt
Posts: 2397
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Story Telling Machine

Mon Jan 14, 2019 11:14 pm

This is probably stating the obvious, but you have considered an MP3 player, right?
You can pick them up for as little as two or three dollars from Ebay etc.

If you get something like an iPod shuffle or replica, you don't get a screen.
Google is ubiquitous - Try it today, it's free!
https://opensource.com/life/16/10/how-ask-technical-questions

User avatar
paddyg
Posts: 2315
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Story Telling Machine

Tue Jan 15, 2019 12:28 am

I think this person viewtopic.php?f=91&t=74875&p=537588&hil ... 21#p537588 was trying to do something similar (it was for a disabled sister I think) and got it working using mpg321. If I get a chance tomorrow I will have a look at the code and post an update - I don't know what the specs are for the phat and whether it works with mgp321, buttons to turn it off etc.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

bertlea
Posts: 265
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Story Telling Machine

Tue Jan 15, 2019 7:47 am

@DestinysOtherChild The only thing I am not sure is why you want it picks a story randomly. What will happen it picks the story it just finished? :roll:

User avatar
paddyg
Posts: 2315
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Story Telling Machine

Tue Jan 15, 2019 8:08 am

@bertlea that specific issue is usually solved by random.shuffle() then iterating in ascending order, possibly reshuffling if the new 0th happens to be same as previous last.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2315
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Story Telling Machine

Tue Jan 15, 2019 1:16 pm

This is a rudimentary script to play mp3 files sequentially. You will have to:
$ sudo apt-get install mpg321
before you run it.

Code: Select all

import os
import subprocess
import random

DIR = '/home/patrick/Music/trench_digital_album' # I'm starting to really like the latest 21 pilots album!

files = [os.path.join(DIR, f) for f in os.listdir(DIR)]

try:
    while True:
        random.shuffle(files)
        for f in files:
            subprocess.call(['mpg321', f])
except KeyboardInterrupt:
    # do any tidying up here - not sure if anything's needed
    pass
It doesn't have anything sophisticated like volume control or skip ahead or back. These are possible with mpg321 but it has to be set up differently (as per previous link).
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

DestinysOtherChild
Posts: 2
Joined: Mon Jan 14, 2019 8:34 pm

Re: Story Telling Machine

Tue Jan 15, 2019 5:35 pm

Wow! Thanks for all the replies!

I looked at your suggestions and went back to the drawing board. I decided to use the pygame music player instead of continuing to struggle with VLC. I found some really helpful commands on Stackoverflow and last night (only about 4 hours after I should have been asleep), I got it working!

I have to write a little more code this evening. I will post the results when I finish them.

Sincerest thanks!

Return to “Python”