November 22, 2024, 11:58:13 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


Random voices

Started by time2dive, April 25, 2009, 02:23:52 AM

Previous topic - Next topic

time2dive

I want to play a random mp3 file 1 of 10 with a VM2.  I now know how to make the VM2 work.  What I need to learn is how to generate a random number from 1-10 after a trigger is triggered and play voice(x).mp3 were x is the random number.

This is what I have so far.....


Tim



' =========================================================================
'
'  ' =========================================================================
'
'   File...... Tim02--Talking Skulls.BS2
'   Purpose...
'   Author.... Timothy Ewing
'   E-mail.... time2dive@hawaii.rr.com
'   Started... 24 Feb 09
'   Updated... 24 Apr 09
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Talking Skulls controller
' output vMusic
' output light
' output skull servos


' -----[ I/O Definitions ]-------------------------------------------------
' -----[ I/O Definitions ]-------------------------------------------------

Sio               PIN     15                      ' no ULN / SETUP = UP
Trigger         PIN     14                      ' no ULN / SETUP = DN
TX                PIN     13                      ' no ULN / SETUP = UP
RX                PIN     12                      ' no ULN / SETUP = UP

'
'
'
' Note: The P1T button is used to simulate the PIR sensor at this time




Pir                PIN     6                    ' PIR sensor
spots           PIN     4                    ' spot lights are connected to P4

' -----[ Constants ]-------------------------------------------------------

IsOn            CON     1
IsOff            CON     0
t9600          CON  84
open           CON  $8000
baud           CON  open + t9600


' -----[ Program Code ]----------------------------------------------------

PAUSE 4000                                                       'allow VMusic2 to initialize

Main:
  IF (Pir = IsOff) THEN Main                                 ' wait for PIR activity
  HIGH spots                                                       ' turn on spot lights

insert random generator

  SEROUT 13, baud, ["VSV $00", 13]                   ' set volume full (in hex), ' insert carriage return
  SEROUT 13, baud, ["VPF voice(x).mp3", 13]     ' play voice(x).mp3, insert carriage return

JonnyMac

April 25, 2009, 08:40:36 AM #1 Last Edit: April 25, 2009, 08:42:53 AM by JonnyMac
This will do what you want: it will play voice01.mp3, voice02.mp3, voice03.mp3 ... voice10.mp3; note that the files less than 10 have a leading zero in the name. 

Also note that I'm using the RX input to detect the presence of the VM2 and the start and end of the audio; this is the only reliable way to do what you want.  Please trust me on this; the VM2 is junk (my opinion) and you cannot count on it unless you're getting feedback from it.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Sio             PIN     15                      ' no ULN / SETUP = UP
Trigger         PIN     14                      ' no ULN / SETUP = DN
TX              PIN     13                      ' no ULN / SETUP = UP
RX              PIN     12                      ' no ULN / SETUP = UP

PIR             PIN     6                       ' PIR sensor
Spots           PIN     4                       ' Spot lights


' -----[ Constants ]-------------------------------------------------------

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0

Yes             CON     1
No              CON     0

T2400           CON     396
T9600           CON     84
T19K2           CON     32
T38K4           CON     6

SevenBit        CON     $2000
Inverted        CON     $4000
Open            CON     $8000
Baud            CON     Open + T9600


' -----[ Variables ]-------------------------------------------------------

timer           VAR     Byte

lottery         VAR     Word
theMP3          VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  PAUSE 3000                                    ' let VMUSIC power up

  SEROUT TX, Baud, [CR]                         ' ping (if no power-up)
  GOSUB VM_Wait_Prompt
  GOSUB VM_Stop                                 ' stop if playing


' -----[ Program Code ]----------------------------------------------------

Main:
  timer = 0                                     ' reset timer
  DO WHILE timer < 100                          ' wait for 0.1s input
    RANDOM lottery                              ' stir random #
    PAUSE 5
    timer = timer + 5 * PIR
  LOOP

  theMP3 = lottery // 10 + 1                    ' random, 1 to 10


Play_MP3:
  SEROUT TX, Baud, ["VPF voice", DEC2 theMP3, ".mp3", CR]
  GOSUB VM_Wait_Start

  DEBUG "Playing: voice", DEC2 theMP3, ".mp3", CR

  GOSUB VM_Wait_Prompt                          ' let it finish
  PAUSE 1000                                    ' miminun delay
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------

VM_Wait_Prompt:
  SERIN RX, Baud, [WAIT(">")]
  RETURN

' -------------------------------------------------------------------------

VM_Stop:
  SEROUT TX, Baud, ["VST", CR]
  RETURN

' -------------------------------------------------------------------------

VM_Wait_Start:
  SERIN RX, Baud, [WAIT("T $0")]
  RETURN

' -------------------------------------------------------------------------


' -------------------------------------------------------------------------


' -------------------------------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office