Shop OBEX P1 Docs P2 Docs Learn Events
HDAudio_ADCtoDAC_Demo - ES control don´t communicate with the Demo — Parallax Forums

HDAudio_ADCtoDAC_Demo - ES control don´t communicate with the Demo

I have run the jm_p2-es_matrix_control_demo and the "jm_p2-es_control" work great but in the HDAudio object it didn´t work. I would like know how it can be solved.
Thanks in advance.

Comments

  • JonnyMacJonnyMac Posts: 9,159
    edited 2023-01-15 15:06

    ?

  • VonSzarvasVonSzarvas Posts: 3,487
    edited 2023-01-16 15:24

    Make sure in the demo main() function you have selected (ie. Uncommented) the demo that uses the control buttons. It's the last demo.

    That is explained in the video and also in the code comments, so read ALL the comments at the main function, and shout back if you need any more help.

  • I am using this version: Updated.... 18 OCT 2022

    pub DemoWithLoopEffects(channel) | ct, delay, delay_mS, c, f, left, right, i, volume, vlu, vld

    hid.start(BASEPIN_CONTROL)
    ak5704.startx(BASEPIN_ADC, BASEPIN_DAC, P_DAC_75R_2V, 2)

    f := 0
    c := 0
    delay := (BUF_SIZE / 2) - 2
    ...

  • Are your basepin settings correct?

    That code certainly works. If you need help with your version of the code, please use the PropellerTool file menu to create a code archive (without tool) and post it here.

    Could just be you have something nudged out of place.

  • masselonmasselon Posts: 51
    edited 2023-01-16 14:54

    @VonSzarvas Posts: 2,942 2023-01-15 19:42 Flag
    Are your basepin settings correct?

    Yes!

    '' =================================================================================================
    ''
    ''   File....... 64014_HDAudio_ADCtoDAC_Demo.spin2
    ''   Purpose.... Demo for Parallax P2 Audio Add-on Set #64014
    ''   Author..... Michael Mulholland
    
    ''   Updated.... 18 OCT 2022
    ''
    '' =================================================================================================
    
    
    '' Demo Hardware Setup
    '' -------------------
    ''
    '' 64014A (ADC) connected at Basepin 24
    '' 64014D (DAC) connected at Basepin 16
    '' 64006a (CONTROL) button & LED add-on connected at Basepin 8  (OPTIONAL!)
    ''
    '' Play sound into the ADC board Line In jack
    '' Listen on headphones connected to the DAC jack
    '' Optionally, use the buttons on the control board to loop backwards or forwards, or adjust volume up/down (functions active when button LEDs lit)
    ''
    '' Refer to the Main() function below, to comment/uncomment different demos
    
    
    
    con { timing }
    
      _xinfreq =  20_000_000
      _clkfreq = 288_000_000 ' 48kHz * 6000
    
    
    con { app io pins }
    
      BASEPIN_ADC     = 24
    
      BASEPIN_DAC     = 16
    
      BASEPIN_CONTROL = 8
    
    
    com { general settings }
    
      BUF_SIZE        = 96000  ' Reduce this value if not enough memory to compile. For more space, consider the Edge 32MB PSRAM as a buffer!
    
      HID_LOOP_BTN    = 0
      HID_REV_BTN     = 1
      HID_VOLUP_BTN   = 2
      HID_VOLDN_BTN   = 3
    
    obj
    
      ak5704 : "AK5704_Driver"
      hid    : "jm_p2-es_control"
    
    var
    
      long bufhead, buftail          ' Audio buffer head and tail indexes, and current buffer length
      long buf[BUF_SIZE]             ' Audio buffer
    
      long delay_ct
      byte flag_delay
    
    
    
    pub Main() | value , x, f
    
    
      ''' DemoLineIn(delay_size) will stream stereo samples directly from ADC (Line In) to DAC
      ''' - Set delay-size to 0 for no delay, or a value up to the available buffer size to add a delay to the playback output stream
      '''
      ''' - Connect your audio input to the ADC line input channel, at either the jack socket (or the optional 3-pin 0.1" header pads)
      ''' - Listen on headphones connected to the DAC output jack (or the optional 3-pin 0.1" header pads)
    
      'DemoLineIn(0)           ' Start demo without delay effect
    
      DemoLineIn(BUF_SIZE)    ' Start demo with delay effect, set to maximum duration delay according to buffer size
    
    
    
    
      ''' DemoMicIn(delay_size) will stream stereo samples directly from ADC (Mic In) to DAC
      ''' - Set delay-size to 0 for no delay, or a value up to the available buffer size to add a delay to the playback output stream
      '''
      ''' - Connect your audio input to the ADC microphone input channel, at either the jack socket (or the optional 3-pin 0.1" header pads)
      ''' - Listen on headphones connected to the DAC output jack (or the optional 3-pin 0.1" header pads)
    
      'DemoMicIn(BUF_SIZE)
    
    
      ''' DemoWithLoopEffects(input_channel) will buffer samples from ADC and allow the user to play a loop forwards or in reverse.
      ''' - Set input_channel to either input channel, ak5704.INPUT_MIC (mic/line input jack) or ak5704.INPUT_LINE (line input jack)
      '''
      ''' - To play the effects, hold down one of the buttons on the P2 Control accessory. The available buttons will have an LED indicator lit.
      ''' - Releasing the button returns the audio playback stream to normal playback mode
      '''
      ''' - Button 0 = Loop Forward
      ''' - Button 1 = Loop Reverse
      ''' - Button 2 = Volume Up
      ''' - Button 3 = Volume Down
      '''
      ''' - Connect your audio input to the ADC line input channel, at either the jack socket (or the optional 3-pin 0.1" header pads)
      ''' - Listen on headphones connected to the DAC output jack (or the optional 3-pin 0.1" header pads)
    
      'DemoWithLoopEffects(ak5704.INPUT_LINE)
    
    
    
    pub DemoLineIn(delaysize) | left, right
    
      ' Optional - set alternative DAC base drive strength (impedance). Default is 124R, equating to ~31ohms- ideal for many headphones
      ' ak5704.setDAC_BaseImpedance(P_DAC_124R_3V) ' P_DAC_990R_3V, P_DAC_600R_2V, P_DAC_124R_3V, P_DAC_75R_2V
    
      ' Start the audio driver
      ak5704.start(BASEPIN_ADC, BASEPIN_DAC)
    
      repeat
    
        ' Read left and right samples from Line in jack
        left, right := ak5704.getSampleFromADC(ak5704.INPUT_LINE)
    
        ' Optional, insert audio processing / effect
        if delaysize > 0
          left, right := addDelay(delaysize, left, right)
    
        ' Write the sample to the DAC output queue
        ak5704.sendSampleToDAC(left, right)
    
    
    pub DemoMicIn(delaysize) | left, right
    
      ' Start the audio driver
      ak5704.start(BASEPIN_ADC, BASEPIN_DAC)
    
      ' Optionally set microphone phantom power, and enable the output
      ' Note: Without enabling the output, the mic input behaves like a second line input channel!
    
      ak5704.setMicSensitivity(128, ak5704.INPUT_MIC, ak5704.CH_LR) ' value of 128 is default with 0dB gain
    
      ak5704.setMicPower(ak5704.MICPWR_1800mV)              ' MICPWR_2800mV, MICPWR_2500mV, MICPWR_1800mV, MICPWR_3000mV
      ak5704.setMicPowerOnOff(ak5704.CH_LR, ak5704.ON)      ' Phantom power can be enabled on either CH_L, CH_R or both channels with CH_LR
    
    
      repeat
    
       ' Read left and right samples from Line in jack
        left, right := ak5704.getSampleFromADC(ak5704.INPUT_MIC)
    
        ' Optional, insert audio processing / effect
        if delaysize > 0
          left, right := addDelay(delaysize, left, right)
    
        ' Write the sample to the DAC output queue
        ak5704.sendSampleToDAC(left, right)
    
    
    
    
    pub DemoWithLoopEffects(channel) | ct, delay, delay_mS, c, f, left, right, i, volume, vlu, vld
    
      hid.start(BASEPIN_CONTROL)
      ak5704.startx(BASEPIN_ADC, BASEPIN_DAC, P_DAC_75R_2V, 2)
    
      f := 0
      c := 0
      delay := (BUF_SIZE / 2) - 2
    
      ct := getct()
    
      hid.wr_led(HID_LOOP_BTN, 1)                           ' set LED on
      hid.wr_led(HID_REV_BTN, 1)                            ' set LED on
    
      hid.wr_led(HID_VOLUP_BTN, 1)                          ' set LED on
      hid.wr_led(HID_VOLDN_BTN, 1)                          ' set LED on
    
      volume := 0
      vlu := false                                          ' volume control locks (could also be coded in another cog)
      vld := false
    
      ak5704.setAmplifierGain(volume, channel)
    
    
     '' Fill inital buffer, leaving space for 4 samples
      repeat until c++ == (BUF_SIZE/2) -4
    
        left, right := ak5704.getSampleFromADC(channel)
        writeBuffer(left, right)
    
      delay_mS := MULDIV64(getct()-ct, 1_000_000, clkfreq) / 1000
      debug("Start with ", udec(delay_mS, --c, getBufferLength()))
    
    
       '' Main loop
      repeat
    
        if hid.rd_btn(HID_LOOP_BTN) == 1
          loopForward(channel)
    
        if hid.rd_btn(HID_REV_BTN) == 1
          loopReverse(channel)
    
        if ( !vlu AND hid.rd_btn(HID_VOLUP_BTN) == 1 )
          ak5704.setAmplifierGain(++volume, channel)
          vlu := true                                       ' Wait until user is no longer pressing the volume button!
    
        if ( !vld AND hid.rd_btn(HID_VOLDN_BTN) == 1 )
          ak5704.setAmplifierGain(--volume, channel)
          vld := true                                       ' Wait until user is no longer pressing the volume button!
    
        volume := 0 #> volume <# 10                         ' Limit volume range
    
         ''Reset volume locks?
        vlu := (hid.rd_btn(HID_VOLUP_BTN) == 0) ? false : true
        vld := (hid.rd_btn(HID_VOLDN_BTN) == 0) ? false : true
    
    
        ''read buffered sample and send to the DAC for playback
        left, right := readBuffer()
        ak5704.sendSampleToDAC(left, right)
    
        '' get next sample from ADC and save to buffer
        '' - option to add effects to left and right samples before writing them to buffer!
        left, right := ak5704.getSampleFromADC(channel)
        writeBuffer(left, right)
    
    
    pri writeBuffer(left, right)
    
      buf[bufhead] := left                                  ' move to buffer
      if (++bufhead == BUF_SIZE)                            ' update head pointer
        bufhead := 0
    
      buf[bufhead] := right                                 ' move to buffer
      if (++bufhead == BUF_SIZE)                            ' update head pointer
        bufhead := 0
    
    
    pri readBuffer() : left, right
    
      repeat while (buftail == bufhead)                     ' hold while buffer empty
    
      left := buf[buftail]                                  ' get a byte
      if (++buftail == BUF_SIZE)                            ' update tail pointer
        buftail := 0
    
      right := buf[buftail]                                 ' get a byte
      if (++buftail == BUF_SIZE)                            ' update tail pointer
        buftail := 0
    
    
    pri getBufferLength() : value
    
      if (buftail < bufhead)
    
          return bufhead - buftail
    
      else
    
          return (BUF_SIZE - buftail) + bufhead
    
    
    pri readBufferReverse() : left, right
    
      repeat while (buftail == bufhead)                     ' hold while buffer empty
    
      left := buf[buftail]                                  ' get a byte
      if (--buftail == 0)                                   ' update tail pointer
        buftail := BUF_SIZE
    
      right := buf[buftail]                                 ' get a byte
      if (--buftail == 0)                                   ' update tail pointer
        buftail := BUF_SIZE
    
    
    ' Audio Effects
    
    
    pri addDelay(delaysize, left, right) : delayedleft, delayedright | bufferedStereoSamples, delay_uS
    
      ' Store new sample in circular buffer, and return oldest sample from the buffer
      ' Buffer size sets the maximum delay size
    
      ' Optional - for debug, store the clock counter value when first calling this function
      if (flag_delay == 0)
          delay_ct := getct()
          flag_delay++
    
      ' Optional - ensure delaysize is within a valid range
      delaysize := 0 #> delaysize <# BUF_SIZE/2             ' Limit delaysize range to the available buffer size (divide buffer by 2, to provide space for both the left and right samples)
    
      ' Optional - provide quick return path if delaysize is 0
      if delaysize == 0
        return left, right                                  ' Do nothing, return immediately
    
    
      ' Write new sample to delay buffer
      writeBuffer(left, right)
    
      ' Check if delay buffer contains at least delaysize samples (ie. delay buffer full and ready to play samples)
      if (getBufferLength()/2 > delaysize-2)
    
        ' Optional - for debug, display the buffer size and delay time when first reaching the delay buffer full state
        if (flag_delay == 1)
          bufferedStereoSamples := getBufferLength()/2 + 1
          delay_uS := MULDIV64(getct()-delay_ct, 1_000_000, clkfreq) / 1000
          debug("Delay buffer ready. ", udec(bufferedStereoSamples, delay_uS))
          flag_delay++
    
        ' If buffer is full, grab a sample from the buffer
        delayedleft, delayedright := readBuffer()           ' grab oldest buffered sample
    
      else
    
        ' Otherwise, don't return a sample- ie. still waiting for buffer to fill up
        delayedleft  := 0
        delayedright := 0
    
      ' Samples stored in delayedleft and delayedright will be returned to the calling code
    
    
    pri loopForward(channel) | left, right, tail
    
      tail := bufhead+2
    
      debug("loopForward")
    
      repeat while hid.rd_btn(HID_LOOP_BTN) == 1
    
            left, right := readBuffer()                     ' Get buffered sample
    
            ak5704.getHeartbeat()                           ' Wait for next sample (hearbeat)
            ak5704.sendSampleToDAC(left, right)             ' Send sample from buffer loop to DAC
    
            if (buftail == bufhead)                         ' No more samples, so re-loop!
              buftail := tail
    
            waitus(18)
    
    
    pri loopReverse(channel) | left, right, tail
    
      tail := bufhead-2
    
      debug("loopReverse")
    
      repeat while hid.rd_btn(HID_REV_BTN) == 1
    
            left, right := readBufferReverse()              ' Get buffered sample
    
            ak5704.getHeartbeat()                           ' Wait for next sample (hearbeat)
            ak5704.sendSampleToDAC(left, right)             ' Send sample from buffer loop to DAC
    
            if (buftail == bufhead)                         ' No more samples, so re-loop!
              buftail := tail
    
            waitus(18)
    
    
  • VonSzarvasVonSzarvas Posts: 3,487
    edited 2023-01-16 15:25

    @masselon said:
    @VonSzarvas Posts: 2,942 2023-01-15 19:42 Flag
    Are your basepin settings correct?

    Yes!

    Hi ! I've added three backticks around your code post above, so it's displayed as code.

    To enable the demo with the control pad, do this....

    Refer to this line in the main() function:

    DemoLineIn(BUF_SIZE)

    Add a single quote to comment out that demo, ie.
    'DemoLineIn(BUF_SIZE)

    Then find this a few lines down:

    'DemoWithLoopEffects(ak5704.INPUT_LINE)

    Remove the tick to uncomment (and thus enable) that demo, ie.
    DemoWithLoopEffects(ak5704.INPUT_LINE)

    Also, unrelated, near the top, this line:
    com { general settings }

    should be:

    con { general settings }

  • @VonSzarvas

    Great! Worked very well.
    Thank very much.

Sign In or Register to comment.