Shop OBEX P1 Docs P2 Docs Learn Events
SPIN WAV Player — Parallax Forums

SPIN WAV Player

RaymanRayman Posts: 13,767
edited 2007-11-10 06:13 in Propeller 1
Ok, it only plays mono and at a fairly low sample rate of 16 ksps.

But, for SPIN, I think it's pretty impressive!

Fairly light on used resources too... (so can be included in other programs...)

I've included a few samples.

Files must be PCM WAV mono @ 16000 to play right

You'll need an SD card installed to use this...· (because it plays from the SD card)

Comments

  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-11-09 17:06
    Sweet! I'm looking forward to trying this tonight!

    Oldbitcollector

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

    — Calvin, of 'Calvin and Hobbes.
  • RaymanRayman Posts: 13,767
    edited 2007-11-09 18:50
    Here's a minimal version without VGA output.

    If you already have Rockiki's SD card object in your project, this code only adds 334 longs...
  • RaymanRayman Posts: 13,767
    edited 2007-11-09 20:37
    Here's another sample...· About 1 minute clip from Feist's "1234".

    File size at this rate is about 2 MB/minute...· So, a 1 GB SD card should hold 8 hours of sound.


    Also, I made a few notes here:
    http://www.rayslogic.com/propeller/Programming/dac/dac.htm


    Post Edited (Rayman) : 11/9/2007 8:54:41 PM GMT
  • Spork FrogSpork Frog Posts: 212
    edited 2007-11-09 22:09
    Just tried it out, works great! A little muddy sounding on my speakers, but I know that's a combination of a lot of different things (mainly really cheap speakers.)

    Looking at your code, I didn't realize doing something like this was just that simple.
  • RaymanRayman Posts: 13,767
    edited 2007-11-10 00:02
    Thanks for trying it. As far as quality goes, I used the Demo board and powered speakers and the file sounded (to me) exactly the same as it did when played by my computer. Of course, the fairly low sample rate does reduce the quality a bit.

    Simplicity was the goal with this code. I want to include some sounds in an already bloated project, so it had to be small. Still, there are a few tricks in there that my not be so obvious...
  • Harrison.Harrison. Posts: 484
    edited 2007-11-10 01:40
    It works great! I just resampled 4 mp3s into 'PCM signed 16bit mono @ 16ksps' and it sounds great! In fact, this sounds better then the music coming from games like Counter-Strike 1.6 / HL1.

    Now I'll have to figure out how to use this (might be a nice thing to put under the Christmas tree or something).

    Harrison

    Post Edited (Harrison.) : 11/10/2007 1:45:17 AM GMT
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-11-10 05:21
    Works perfectly! File sounds exactly as it did on the PC. I'm getting a couple initial clicks at the
    beginning of the sound file now when I reduced the buffer size, but otherwise perfect!

    Now I've just got to figure out the changes that the Femto version of fsrw has to place this into PropDOS.

    The propeller just blows me away... [noparse]:)[/noparse]

    Oldbitcollector

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

    — Calvin, of 'Calvin and Hobbes.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-11-10 06:13
    @Rayman:

    Is there any other way I can detect for the end of the wav file? I seem to be loosing it in PropDOS for some reason.

    your current line is: repeat while (j==buffsize*2) 'repeat until end of file

    It looks like the end is never found... Is there an alternative I can set a watchpoint for?

    updated

    Ah ha!

    j > 0 works just fine...


    Thanks
    Oldbit

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

    — Calvin, of 'Calvin and Hobbes.

    Post Edited (Oldbitcollector) : 11/10/2007 6:38:15 AM GMT
  • Ok, I know I am late to the party, but I am new to the Prop and working on a project which includes some wav format sounds.
    I copied the code from the above into my spin code and it works great the first time the "routine"/"method"/"sub" is called, but locks up if it is called again.

    Can someone point me in the right direction here?
    thanks,
    Xy
    PUB playWAV (soundName) | i, n, j
      'Play a WAV File
      term.Str(string(13,"Opening WAV File: "))
      term.Str(soundName)
      term.Str(string(13))
      'open the WAV file  (NOTE:  Only plays mono, 16000 ksps PCM WAV Files !!!!!!!!!!!!!)
      i:=sd.popen(soundName, "r")
    
      'ignore the file header (so you better have the format right!)
      'See here for header format:  http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
      i:=sd.pread(@MonoData, 44) 'read data words to input stereo buffer
    
    '  return
      term.Str(string("Playing: "))
      term.Str(soundName)
      term.Str(string(13))
    
      'Start the player in a new cog
      COGNEW(Player(SPKR_RT, SPKR_LT),@stack1)      'Play runs in a seperate COG (because SPIN is a bit slow!!)
    
      'Keep filling buffers until end of file
      ' note:  using alternating buffers to keep data always at the ready...
      n:=buffSize-1
      j:=buffsize*2
      repeat while (j==buffsize*2)  'repeat until end of file
        if (buff1[n]==0)
          j:=sd.pread(@MonoData, buffSize*2) 'read data words to input stereo buffer
          'fill table 1
          repeat i from 0 to n
             buff1[i]:=($8000+MonoData[i])<<16
        if (buff2[n]==0)
          j:=sd.pread(@MonoData, buffSize*2) 'read data words to input stereo buffer
          'fill table 1
          repeat i from 0 to n
             buff2[i]:=($8000+MonoData[i])<<16
    
    
      'must have reached the end of the file, so close it
      sd.pclose
    
  • Ok, so as murphy's law must dictate, as soon as I asked I realized what was happening, thanks anyway, got it going :smile:
  • @Xyvius
    Murphy's always there, just waiting for the right moment :smiley:
    Do you mind sharing what you did originally vs what worked?

    Cheers


  • @avsa242
    Sorry for the delay in my reply
    I don't mind at all, simply changed over to use the COGINIT instead of the COGNEW command and specified a cog to use. Then stopped the cog after the wav file is closed. Worked from there.

    I have however hit another snag, my understanding of wav playback is VERY limited and I don't see a way to adjust volume control from code. The code I am working on would really benefit from it if anyone wants to point me in the right direction.

    Thanks
  • RaymanRayman Posts: 13,767
    I guess you can change the volume of the file with a computer

    Otherwise you need to multiply the samples by a scaling factor...
  • kwinnkwinn Posts: 8,697
    You can also use a digital potentiometer to control the volume. See https://www.parallax.com/search?search_api_views_fulltext=digital+potentiometer
Sign In or Register to comment.