Page 1 of 1

Play Music continued...

Posted: Tue Jan 22, 2019 6:59 pm
by Tffsnyder
This is a continuation of the post I put into programs by mistake.

I have made corrections and I am now running the following program which no longer returns an error message but there is no sound.

BEEP ' to see if beep comes through speaker

M$ = "tune"
MUSIC M$ LOAD "Personk.wav"

MUSIC M$ VOLUME 1
MUSIC M$ PLAY

PRINT "Music playing?"; MUSIC_PLAYING (M$)
PRINT "Music length?"; MUSIC_LENGTH (M$)

-------- and this is the resulting output (with no music but with a beep) ————-

Music playing? 1
Music length? 8.54932

Thanks in advance.

Tom

Re: Play Music continued...

Posted: Tue Jan 22, 2019 10:28 pm
by rbytes
Your program ends too soon for the sound to be heard. Try adding a PAUSE 10 at the end of your code, which should give it time to play the complete sound. Or you could make the pause line PAUSE MUSIC_LENGTH (M$) or PAUSE 8.54932, since that is the length of the sound.

Another little trick is to add a wait loop at the end. "wait" is not a special command, just a label name. But with it and the following two lines, you create a closed loop that isn't broken until you touch the screen. When you do, tx then changes to the x coordinate of the point you touched. Since tx is no longer -1, the loop is broken and your program ends. SLOWDOWN slows down the loop execution so it doesn't run the processor at full speed.

wait: SLOWDOWN
GET TOUCH 0 as tx,ty
IF tx=-1 then wait
END

Re: Play Music continued...

Posted: Wed Jan 23, 2019 12:44 am
by Tffsnyder
Brilliant. I couldn't be more grateful. Of course the program needs to stay on!

I am working with developers at Harvard School of Ed on prototyping some exciting news designs. We are finding Smart Basic to be the only useful tool for quick experiments.

Tom

Re: Play Music continued...

Posted: Wed Jan 23, 2019 3:19 am
by rbytes
It is quite amazing what can be programmed with it. Too bad Apple doesn't value this developer as much as we who have discovered the power of Smart Basic.

Good luck with your projects. Let me know if I can be of further help.

Re: Play Music continued...

Posted: Thu Jan 24, 2019 6:53 pm
by GeorgeMcGinn
Tom, rbytes,

You could also use the statement: MUSIC_PLAYING (M$)

You can use rbytes loop, but instead of on even in addition to, add:
IF MUSIC_PLAYING(M$) THEN wait

MUSIC_PLAYING returns 1 if musical composition [m$] is currently playing. Otherwise returns 0. So all you need is to test if true, or not = 0.

So the loop would look something like this:
wait: SLOWDOWN
GET TOUCH 0 as tx,ty
IF tx=-1 AND MUSIC_PLAYING(M$) THEN wait
END
It looks like if you touch the screen, that stops the music file from playing, So you need to make sure both test true to go back to the top of your loop. There may be a more elegant way of coding this, but I think you get the message. If either or both a touch or the music ends will exit the loop.

However, if the MUSIC is anything like the SAY statement, I would add a MUSIC_STOP(M$) before the END statement, just to make sure the song actually stops playing. On rare occasions my weather app will continue saying the forecast, even if I put in a PAUSE or the program ends.

Hope this helps,

Re: Play Music continued...

Posted: Thu Jan 24, 2019 7:46 pm
by rbytes
That is a big improvement, because it lets you end the music at any time. If you used a pause the length of the song, you couldn't do that. Good call, George.

Re: Play Music continued...

Posted: Fri Jan 25, 2019 4:33 pm
by matt7
Haven't been able to do much coding with Smart Basic in several months, but I check in on this forum from time to time.

I will often have a debug exit condition similar to the code above except I like to use TOUCH 1 (instead of TOUCH 0). This means you must touch the screen with two fingers to trigger a debug exit, which helps if you need to touch the screen for normal interaction with the program. My main program loop would look something like this:

Code: Select all

' setup code

menuExit = 0
debugExit = 0
DO

  ' processing
  
  IF TOUCH_X(1) > -1 THEN debugExit = 1
  
UNTIL menuExit OR debugExit

' tear down code
I think this keeps program flow very readable and allows anyone (mainly me in the future!) to quickly look at the code and figure out what is going on.

But for a simple situation like the original question about playing music, I would probably just do this:

Code: Select all

DO
  SLOWDOWN
UNTIL MUSIC_PLAYING(M$) = 0 OR TOUCH_X(1) > -1

M$ = tune.

Posted: Mon Jun 01, 2020 10:25 pm
by Ken
I got these suggested lines to work.

M$ = "tune"

MUSIC m$ LOAD "Bass Drum 1.wav"
MUSIC m$ PLAY

But then I found all I needed was this

MUSIC 1 LOAD "Bass Drum 1.wav"
MUSIC 1 PLAY