# mp3_decoder_vs1053b

vs1053b mp3 decoder sample

Author | Matthew Schinkel, copyright © 2009, all rights reserved.
Adapted-by |
Compiler | >=2.4m

Description

    example usage of the vs1053 mp3 decoder. plays .mp3 files from
                 a fat32 formatted sd card. Full control of play speed, volume,
                 right/left speaker, bass, treble, mute. Also plays
                 Ogg Vorbis/AAC/WMA/MIDI

Sources

    VLSI Solutions Datasheet - www.vlsi.fi/fileadmin/datasheets/vlsi/vs1053.pdf

Notes

    vs1053 uses SPI mode 0,0
    must use 12.288mhz crystal for vs1053b

Dependencies

No dependency found

## Summary

### Global variables/contants

- `var bit vs_pin_gpio3_data at vs_port_gpio_data : 3`
- `var byte vs_current_file_type[4]`
- `var byte _vs_play_speed_data = 1`
- `const byte VS_REG_DECODE_TIME = 0x04`
- `const byte _vs_table_layer2_id3[15] = {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384}`
- `var bit vs_pin_gpio7_direction_data at vs_port_gpio_direction_data : 7`
- `var bit vs_pin_gpio4_direction_data at vs_port_gpio_direction_data : 4`
- `const byte VS_REG_BASS = 0x02`
- `var dword vs_bitrate`
- `var byte _vs_volume_data_both[2] at _vs_volume_data`
- `const byte VS_REG_AIADDR = 0x0A`
- `var bit vs_pin_gpio5_direction_data at vs_port_gpio_direction_data : 5`
- `var bit vs_pin_gpio7_data at vs_port_gpio_data : 7`
- `const byte VS_REG_HDAT1 = 0x09`
- `const byte VS_REG_STATUS = 0x01`
- `const byte VS_REG_VOL = 0x0B`
- `const byte VS_REG_AICTRL2 = 0x0E`
- `var bit vs_pin_gpio6_direction_data at vs_port_gpio_direction_data : 6`
- `const byte VS_REG_AICTRL1 = 0x0D`
- `const byte _vs_table_layer2_id[15] = {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}`
- `const byte VS_REG_WRAM = 0x06`
- `var bit vs_pin_gpio1_direction_data at vs_port_gpio_direction_data : 1`
- `var byte _vs_left_data at _vs_volume_data_both[0]`
- `const byte VS_REG_HDAT0 = 0x08`
- `var bit vs_pin_gpio0_data at vs_port_gpio_data : 0`
- `const byte _vs_table_layer1_id3[15] = {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448}`
- `const byte VS_REG_AICTRL0 = 0x0C`
- `var bit vs_init_ok = FALSE`
- `var byte*2 _vs_volume_data= 0`
- `var sbyte _vs_current_volume_right = 0`
- `var sbyte vs_current_treble = 0`
- `var word vs_samplerate`
- `var sbyte _vs_current_volume_left = 0`
- `var bit vs_pin_gpio6_data at vs_port_gpio_data : 6`
- `const word _vs_samplerate_id1[3] = {8000,12000,11025}`
- `const byte VS_REG_MODE = 0x00`
- `const byte VS_REG_AUDATA = 0x05`
- `var byte vs_port_gpio_data = 0`
- `var byte vs_current_bass = 0`
- `const word _vs_samplerate_id3[3] = {32000,48000,44100}`
- `const byte VS_REG_WRAMADDR = 0x07`
- `const byte _vs_table_layer1_id[15] = {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256}`
- `const byte _vs_table_layer3_id[15] = {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}`
- `const word _vs_samplerate_id2[3] = {16000,24000,22050}`
- `var bit vs_pin_gpio3_direction_data at vs_port_gpio_direction_data : 3`
- `const byte VS_REG_AICTRL3 = 0x0F`
- `var bit _vs_mute_data = FALSE`
- `var bit vs_pin_gpio2_direction_data at vs_port_gpio_direction_data : 2`
- `var bit vs_pin_gpio2_data at vs_port_gpio_data : 2`
- `const byte _vs_table_layer3_id3[15] = {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}`
- `var bit vs_pin_gpio5_data at vs_port_gpio_data : 5`
- `var dword vs_average_bitrate`
- `var bit vs_pin_gpio0_direction_data at vs_port_gpio_direction_data : 0`
- `var byte vs_port_gpio_direction_data = 0`
- `const byte VS_REG_CLOCKF = 0x03`
- `var byte _vs_right_data at _vs_volume_data_both[1]`
- `var byte _vs_current_volume = 0`
- `var bit vs_pin_gpio1_data at vs_port_gpio_data : 1`
- `var bit vs_pin_gpio4_data at vs_port_gpio_data : 4`

### Procedures

- `vs_soft_reset()`
- `vs_patch_vs1053b()`
- `vs_volume_left'put(sbyte in data)`
- `vs_pin_gpio7'put(bit in data)`
- `vs_pin_gpio4'put(bit in data)`
- `vs_play_mode()`
- `vs_stop()`
- `vs_register_read(byte in address,word out data)`
- `vs_pin_gpio4_direction'put(bit in data)`
- `vs_line_in_mode()`
- `vs_pin_gpio5'put(bit in data)`
- `vs_bass'put(byte in data)`
- `vs_pin_gpio3_direction'put(bit in data)`
- `vs_volume_right'put(sbyte in data)`
- `vs_reg_read_to_serial(byte in register)`
- `vs_pin_gpio6'put(bit in data)`
- `vs_pin_gpio7_direction'put(bit in data)`
- `vs_pin_gpio0'put(bit in data)`
- `vs_pin_gpio6_direction'put(bit in data)`
- `vs_file_info()`
- `vs_pin_gpio2_direction'put(bit in data)`
- `vs_register_write(byte in address, word in data)`
- `vs_microphone_mode()`
- `vs_pin_gpio3'put(bit in data)`
- `vs_volume'put(byte in data)`
- `vs_play()`
- `vs_port_gpio_direction'put(byte in data)`
- `vs_read_ram(word in address)`
- `vs_mute'put(bit in data)`
- `vs_init()`
- `vs_pin_gpio1_direction'put(bit in data)`
- `vs_play_speed'put(byte in data)`
- `vs_pin_gpio1'put(bit in data)`
- `vs_treble'put(sbyte in data)`
- `vs_port_gpio'put(byte in data)`
- `vs_pin_gpio0_direction'put(bit in data)`
- `vs_pin_gpio5_direction'put(bit in data)`
- `vs_pin_gpio2'put(bit in data)`

Private

- `_vs_send_mp3(byte in data)`
- `_vs_set_bass_treble_set()`
- `_vs_volume_set()`

### Functions

- `vs_pin_gpio4'get() return bit`
- `vs_pin_gpio1'get() return bit`
- `vs_pin_gpio2'get() return bit`
- `vs_pin_gpio3_direction'get() return bit`
- `vs_pin_gpio3'get() return bit`
- `vs_pin_gpio5_direction'get() return bit`
- `vs_bass'get() return byte`
- `vs_pin_gpio6_direction'get() return bit`
- `vs_port_gpio_direction'get() return byte`
- `vs_volume_left'get() return sbyte`
- `vs_treble'get() return sbyte`
- `vs_pin_gpio0_direction'get() return bit`
- `vs_pin_gpio0'get() return bit`
- `vs_pin_gpio2_direction'get() return bit`
- `vs_pin_gpio7_direction'get() return bit`
- `vs_pin_gpio1_direction'get() return bit`
- `vs_volume_right'get() return sbyte`
- `vs_port_gpio'get() return byte`
- `vs_pin_gpio7'get() return bit`
- `vs_pin_gpio4_direction'get() return bit`
- `vs_volume'get() return byte`
- `vs_pin_gpio5'get() return bit`
- `vs_play_speed'get() return byte`
- `vs_pin_gpio6'get() return bit`
- `vs_mute'get() return bit`

## API details

### Global variables/contants

- `var bit vs_pin_gpio3_data at vs_port_gpio_data : 3`

  No documentation found

- `var byte vs_current_file_type[4]`

      holds current file type after vs_file_info()

- `var byte _vs_play_speed_data = 1`

      data variables for internal use only

- `const byte VS_REG_DECODE_TIME = 0x04`

  No documentation found

- `const byte _vs_table_layer2_id3[15] = {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384}`

  No documentation found

- `var bit vs_pin_gpio7_direction_data at vs_port_gpio_direction_data : 7`

  No documentation found

- `var bit vs_pin_gpio4_direction_data at vs_port_gpio_direction_data : 4`

  No documentation found

- `const byte VS_REG_BASS = 0x02`

  No documentation found

- `var dword vs_bitrate`

      you can read these variables after calling vs_file_info()

- `var byte _vs_volume_data_both[2] at _vs_volume_data`

  No documentation found

- `const byte VS_REG_AIADDR = 0x0A`

  No documentation found

- `var bit vs_pin_gpio5_direction_data at vs_port_gpio_direction_data : 5`

  No documentation found

- `var bit vs_pin_gpio7_data at vs_port_gpio_data : 7`

  No documentation found

- `const byte VS_REG_HDAT1 = 0x09`

  No documentation found

- `const byte VS_REG_STATUS = 0x01`

  No documentation found

- `const byte VS_REG_VOL = 0x0B`

  No documentation found

- `const byte VS_REG_AICTRL2 = 0x0E`

  No documentation found

- `var bit vs_pin_gpio6_direction_data at vs_port_gpio_direction_data : 6`

  No documentation found

- `const byte VS_REG_AICTRL1 = 0x0D`

  No documentation found

- `const byte _vs_table_layer2_id[15] = {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}`

  No documentation found

- `const byte VS_REG_WRAM = 0x06`

  No documentation found

- `var bit vs_pin_gpio1_direction_data at vs_port_gpio_direction_data : 1`

  No documentation found

- `var byte _vs_left_data at _vs_volume_data_both[0]`

  No documentation found

- `const byte VS_REG_HDAT0 = 0x08`

  No documentation found

- `var bit vs_pin_gpio0_data at vs_port_gpio_data : 0`

  No documentation found

- `const byte _vs_table_layer1_id3[15] = {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448}`

      bitrate table for .mp3 at 8.7.9 in vs1053b datasheet

- `const byte VS_REG_AICTRL0 = 0x0C`

  No documentation found

- `var bit vs_init_ok = FALSE`

      if initialization was ok, this will change to TRUE

- `var byte*2 _vs_volume_data= 0`

  No documentation found

- `var sbyte _vs_current_volume_right = 0`

  No documentation found

- `var sbyte vs_current_treble = 0`

  No documentation found

- `var word vs_samplerate`

      sample rate (for .mp3 only)

- `var sbyte _vs_current_volume_left = 0`

  No documentation found

- `var bit vs_pin_gpio6_data at vs_port_gpio_data : 6`

  No documentation found

- `const word _vs_samplerate_id1[3] = {8000,12000,11025}`

  No documentation found

- `const byte VS_REG_MODE = 0x00`

      vs1053b registers for use with vs_register_read / vs_register_write procedures

- `const byte VS_REG_AUDATA = 0x05`

  No documentation found

- `var byte vs_port_gpio_data = 0`

      Use vs1053b 8 bit GPIO port & pins like a regular port (3.3v tollerent I/O pins).
      port name is "vs_port_gpio", pin names are "vs_pin_gpio0" to "vs_pin_gpio7"
      add "_direction" to the pin name to set it's direction. (pseudo variables).
      gpio0 & gpio1 must have pull down resistors for active low at startup.

- `var byte vs_current_bass = 0`

      Writes calculated bass and treble data to base/treble register (internal use only)

- `const word _vs_samplerate_id3[3] = {32000,48000,44100}`

      sample rate table for .mp3 at 8.7.9 in vs1053b datasheet

- `const byte VS_REG_WRAMADDR = 0x07`

  No documentation found

- `const byte _vs_table_layer1_id[15] = {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256}`

  No documentation found

- `const byte _vs_table_layer3_id[15] = {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}`

  No documentation found

- `const word _vs_samplerate_id2[3] = {16000,24000,22050}`

  No documentation found

- `var bit vs_pin_gpio3_direction_data at vs_port_gpio_direction_data : 3`

  No documentation found

- `const byte VS_REG_AICTRL3 = 0x0F`

  No documentation found

- `var bit _vs_mute_data = FALSE`

  No documentation found

- `var bit vs_pin_gpio2_direction_data at vs_port_gpio_direction_data : 2`

  No documentation found

- `var bit vs_pin_gpio2_data at vs_port_gpio_data : 2`

  No documentation found

- `const byte _vs_table_layer3_id3[15] = {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}`

  No documentation found

- `var bit vs_pin_gpio5_data at vs_port_gpio_data : 5`

  No documentation found

- `var dword vs_average_bitrate`

      actual average speed of incomming data

- `var bit vs_pin_gpio0_direction_data at vs_port_gpio_direction_data : 0`

  No documentation found

- `var byte vs_port_gpio_direction_data = 0`

  No documentation found

- `const byte VS_REG_CLOCKF = 0x03`

  No documentation found

- `var byte _vs_right_data at _vs_volume_data_both[1]`

  No documentation found

- `var byte _vs_current_volume = 0`

      Writes calculated volume data to volume register (internal use only)

- `var bit vs_pin_gpio1_data at vs_port_gpio_data : 1`

  No documentation found

- `var bit vs_pin_gpio4_data at vs_port_gpio_data : 4`

  No documentation found

### Procedures

- `vs_soft_reset()`

      Perform soft reset.

- `vs_patch_vs1053b()`

      patch for vs1053b only (don't know what it fixes)

- `vs_volume_left'put(sbyte in data)`

      Left speaker volume control (pseudo variable) (-127 to 127) in steps of 0.5db.
      Max volume is 255, so if vs_volume = 250, you may only add 5 more to the
      left speaker (vs_volume_left = 5). Higher values are allowed but will be
      limited by the max volume.

- `vs_pin_gpio7'put(bit in data)`

      pin vs_pin_gpio7

- `vs_pin_gpio4'put(bit in data)`

      pin vs_pin_gpio4

- `vs_play_mode()`

      Set vs back to normal for playing media files

- `vs_stop()`

      clears vs_1053b data input buffer. This should be done after
      sending mp3 data, otherwise you will get some unwanted sound from the
      previous file played.

- `vs_register_read(byte in address,word out data)`

      Read from a register

- `vs_pin_gpio4_direction'put(bit in data)`

  No documentation found

- `vs_line_in_mode()`

      Turn on line in.

- `vs_pin_gpio5'put(bit in data)`

      pin vs_pin_gpio5

- `vs_bass'put(byte in data)`

      Bass enhance/boost (pseudo variable). vs_bass = 0 to 15, 1db per step
      Normal level is vs_bass = 0

- `vs_pin_gpio3_direction'put(bit in data)`

  No documentation found

- `vs_volume_right'put(sbyte in data)`

      Right speaker volume control (pseudo variable) (-127 to 127) in steps of 0.5db.
      Max volume is 255, so if vs_volume = 250, you may only add 5 more to the
      right speaker (vs_volume_right = 5). Higher values are allowed but will be
      limited by the max volume.

- `vs_reg_read_to_serial(byte in register)`

      print the contents of a register to the serial port in decimal

- `vs_pin_gpio6'put(bit in data)`

      pin vs_pin_gpio6

- `vs_pin_gpio7_direction'put(bit in data)`

  No documentation found

- `vs_pin_gpio0'put(bit in data)`

      pin vs_pin_gpio0

- `vs_pin_gpio6_direction'put(bit in data)`

  No documentation found

- `vs_file_info()`

      Get info about music that is currently playing. So far only gets file type
      (MP3, WAV, AAC, WMA, MIDI, OGG)

- `vs_pin_gpio2_direction'put(bit in data)`

  No documentation found

- `vs_register_write(byte in address, word in data)`

      Write to a register

- `vs_microphone_mode()`

      Turn on Microphone.

- `vs_pin_gpio3'put(bit in data)`

      pin vs_pin_gpio3

- `vs_volume'put(byte in data)`

      Volume control (pseudo variable). Sets volume by -0.5db per step down from 255.
      "vs_volume = 253" will give 20 * -0.5 = -10db
      "vs_volume = 255" will be 0db (max volume)
      "vs_volume = 0" will be (min volume/no sound)

- `vs_play()`

      Clear vs1053 registers before playing a file

- `vs_port_gpio_direction'put(byte in data)`

  No documentation found

- `vs_read_ram(word in address)`

      print the contents of a register to the serial port in decimal

- `vs_mute'put(bit in data)`

      Mute volume (pseudo variable) vs_mute = TRUE or FALSE

- `vs_init()`

      Initialize vs1053b and set volume to max. User should lower volume after
      init(). After init() vs_reg_mode_data should be equal to 0x4800 or 0x800

- `vs_pin_gpio1_direction'put(bit in data)`

  No documentation found

- `vs_play_speed'put(byte in data)`

      fast forward a playing audio file (pseudo variable). (0 or 1) = normal
      play speed. 2 = 2x play speed, 3 = 3x play speed, etc.

- `vs_pin_gpio1'put(bit in data)`

      pin vs_pin_gpio1

- `vs_treble'put(sbyte in data)`

      Treble control (pseudo variable). vs_treble = -8 to +7, 1.5db per step.
      Normal level is vs_treble = 0. Don't ask me why this is does not work the
      same as bass control, see datasheet and ask the manufacturer!

- `vs_port_gpio'put(byte in data)`

      port vs_port_gpio

- `vs_pin_gpio0_direction'put(bit in data)`

  No documentation found

- `vs_pin_gpio5_direction'put(bit in data)`

  No documentation found

- `vs_pin_gpio2'put(bit in data)`

      pin vs_pin_gpio2

Private

- `_vs_send_mp3(byte in data)`

      Send mp3 data to vs1053b. This is only an example and should not be used
      to test. You should send 32 bytes at a time after when vs_drq goes high.
      vs_data_chip_select must be low for the entire transfer, but you can put it high
      once in a while for bit sync.

- `_vs_set_bass_treble_set()`

  No documentation found

- `_vs_volume_set()`

  No documentation found

### Functions

- `vs_pin_gpio4'get() return bit`

  No documentation found

- `vs_pin_gpio1'get() return bit`

  No documentation found

- `vs_pin_gpio2'get() return bit`

  No documentation found

- `vs_pin_gpio3_direction'get() return bit`

  No documentation found

- `vs_pin_gpio3'get() return bit`

  No documentation found

- `vs_pin_gpio5_direction'get() return bit`

  No documentation found

- `vs_bass'get() return byte`

  No documentation found

- `vs_pin_gpio6_direction'get() return bit`

  No documentation found

- `vs_port_gpio_direction'get() return byte`

  No documentation found

- `vs_volume_left'get() return sbyte`

  No documentation found

- `vs_treble'get() return sbyte`

  No documentation found

- `vs_pin_gpio0_direction'get() return bit`

  No documentation found

- `vs_pin_gpio0'get() return bit`

  No documentation found

- `vs_pin_gpio2_direction'get() return bit`

  No documentation found

- `vs_pin_gpio7_direction'get() return bit`

  No documentation found

- `vs_pin_gpio1_direction'get() return bit`

  No documentation found

- `vs_volume_right'get() return sbyte`

  No documentation found

- `vs_port_gpio'get() return byte`

  No documentation found

- `vs_pin_gpio7'get() return bit`

  No documentation found

- `vs_pin_gpio4_direction'get() return bit`

  No documentation found

- `vs_volume'get() return byte`

  No documentation found

- `vs_pin_gpio5'get() return bit`

  No documentation found

- `vs_play_speed'get() return byte`

  No documentation found

- `vs_pin_gpio6'get() return bit`

  No documentation found

- `vs_mute'get() return bit`

  No documentation found

## Related samples

Here are the list of samples which use this library:

18f4525 | 18f4525_mp3_decoder_vs1053b_pata_hard_disk.jal
18f4525 | 18f4525_mp3_decoder_vs1053b_sd_card.jal
18f4620 | 18f4620_mp3_decoder_vs1053b_pata_hard_disk.jal
18f4620 | 18f4620_mp3_decoder_vs1053b_sd_card.jal
18f67j50 | 18f67j50_mp3_decoder_vs1053b_sd_card.jal
18f67j50 | 18f67j50_mp3_decoder_vs1053b_pata_hard_disk.jal