Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 40 of 40

Thread: Working with New Audio Objects

  1. #26
    Quote Originally Posted by el_supremo View Post
    block->data[0] to block->data[3] is 8 bytes. Each audio sample is an int16_t not 32-bits.

    Pete
    Audio blocks are represented with this data type, which is a C struct. The only member intended for use in update() is "data", an array of 16 bit integers representing the audio. If "myblock" is a pointer to an audio_block_t, use myblock->data[0] to access the first audio sample, myblock->data[1] to access the second, and so on. The data[] array is always 32 bit aligned in memory, so you can fetch pairs of samples by type casting the address as a pointer to 32 bit data.
    https://www.pjrc.com/teensy/td_libs_...ewObjects.html

    Also when take a single sample using code
    Code:
    void AudioEncoder::update(void)
    {
      audio_block_t *in =NULL;
      in = receiveWritable();
      if (!in) return;
      Serial.println(in->data[100], HEX);
    I get output 32 bits and not 16 bits
    Code:
    FFFFF862
    
    FFFFECA2
    
    FFFFF089
    
    FFFFEB1D
    
    FFFFDF4A

  2. #27
    Senior Member
    Join Date
    Jul 2020
    Posts
    397
    Quote Originally Posted by Nishant View Post
    Also when take a single sample using code
    Code:
    void AudioEncoder::update(void)
    {
      audio_block_t *in =NULL;
      in = receiveWritable();
      if (!in) return;
      Serial.println(in->data[100], HEX);
    I get output 32 bits and not 16 bits
    Code:
    FFFFF862
    
    FFFFECA2
    
    FFFFF089
    
    FFFFEB1D
    
    FFFFDF4A
    You aren't getting 32 bits, its just that println only has a method for int, not int16_t, so the value is implicitly converted from
    16 bit to 32 bit (the natural size of int) by the compiler so it can use the int method on println. If you want to only see
    16 bits in the output you can truncate:

    Code:
      Serial.println(in->data[100] & 0xFFFF, HEX);

  3. #28
    Quote Originally Posted by MarkT View Post
    You aren't getting 32 bits, its just that println only has a method for int, not int16_t, so the value is implicitly converted from
    16 bit to 32 bit (the natural size of int) by the compiler so it can use the int method on println. If you want to only see
    16 bits in the output you can truncate:

    Code:
      Serial.println(in->data[100] & 0xFFFF, HEX);
    I am trying this code but somewhere data is getting corrupted
    Code:
    #include <Arduino.h>
    #include "effect_encoder.h"
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    AESSmall256 aes256;
    
    uint8_t key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    uint8_t ciphertext[256];
    
    
    void AudioEncoder::update(void)
    {
      audio_block_t *in =NULL;
      uint8_t  buffer[16];
      uint8_t * bptr = (uint8_t*) (in->data);
       
      in = receiveWritable();
      if (!in) return;
     // Serial.println(in->data[100] & 0xFFFF, HEX);
      
      for (int i=0; i < AUDIO_BLOCK_SAMPLES/8; i++) 
      {
        
        uint8_t aes_block [16];
        
        for (int j = 0 ; j < 16 ; j++)
        aes_block [j] = *bptr++ ;
        aes256.setKey(key, aes256.keySize());
        aes256.encryptBlock(buffer, aes_block);
        memcpy(ciphertext + (i*16), buffer,16);
    
      }
      memcpy(in->data, ciphertext,256);
      Serial.println(in->data[100], HEX);
      transmit(in);
      release(in);
    }
    output is same where else should be changing
    Code:
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC
    FFFFF0AC

  4. #29
    Quote Originally Posted by MarkT View Post
    You aren't getting 32 bits, its just that println only has a method for int, not int16_t, so the value is implicitly converted from
    16 bit to 32 bit (the natural size of int) by the compiler so it can use the int method on println. If you want to only see
    16 bits in the output you can truncate:

    Code:
      Serial.println(in->data[100] & 0xFFFF, HEX);
    The code below is working perfectly and I don't need to use the modified println too

    Code:
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    
    AESSmall256 aes256;
    
    uint16_t plaintext[128] = {0xc126,
    0x29f7,
    0x5999,
    0x825e,
    0xcc56,
    0x9ca1,
    0xf0e9,
    0x1a12,
    0xf1e7,
    0x52b6,
    0xa8e6,
    0x7233,
    0x7235,
    0x0f94,
    0x690f,
    0x507a,
    0x7cb3,
    0xe651,
    0xd0a7,
    0x6130,
    0x6e8a,
    0xd554,
    0x7ce2,
    0x8ece,
    0x62de,
    0x5e91,
    0xb845,
    0xfcc5,
    0x62d6,
    0xa366,
    0xcb00,
    0x0ea6,
    0x1600,
    0x52b4,
    0x72f9,
    0xece0,
    0xf444,
    0x5c18,
    0x0df5,
    0x0261,
    0xe604,
    0x69dc,
    0x7b9f,
    0xdcf3,
    0x292a,
    0x6305,
    0x21e0,
    0x7c1b,
    0x0dc6,
    0x0fd4,
    0x75f9,
    0xe663,
    0xa6d4,
    0x2957,
    0x07f7,
    0xf706,
    0x9e2a,
    0x8c1b,
    0x190e,
    0x0e23,
    0x3391,
    0xcc30,
    0xc724,
    0xd415,
    0x3d28,
    0x9997,
    0xf8c0,
    0xe66d,
    0x6326,
    0x57cb,
    0x7e2b,
    0x29d4,
    0x2681,
    0xb9d6,
    0xda2f,
    0x4829,
    0x8442,
    0xd043,
    0xeb78,
    0xf121,
    0x0d99,
    0x1a59,
    0x0bc9,
    0xafa7,
    0x5e73,
    0xe362,
    0x5dfc,
    0x0aaa,
    0x4320,
    0x8928,
    0x3a85,
    0xa25a,
    0x5d07,
    0x73f4,
    0xcee6,
    0x8f86,
    0xcf45,
    0x5635,
    0x87dd,
    0xfe41,
    0x52cb,
    0x5418,
    0xf7e7,
    0x8ff6,
    0xd67e,
    0x7692,
    0xfe41,
    0xccd8,
    0x0c29,
    0xbc57,
    0x0910,
    0x00d2,
    0x0c39,
    0xfc2e,
    0xd0ef,
    0x0a52,
    0x09a4,
    0xd577,
    0x05a6,
    0x0199,
    0xd918,
    0x0a79,
    0x0bf9,
    0x04a1,
    0x06fe,
    0x007e,
    0x0516,
    0x01e8};
    
    
    uint16_t plaintext2[128];
    uint16_t ciphertext2[128];
    uint8_t  buffer[16];
    uint8_t  buffer1[16];
    
    uint8_t ciphertext[256];
    uint8_t plaintext1[256];
    int count =0 ;
    
    
    uint8_t  key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    uint8_t * bptr = (uint8_t*) (plaintext);
    uint8_t * bptr1 = (uint8_t*) (ciphertext2);
    
    void setup()
    {
        
        Serial.begin(9600);
        delay(1000);
        Serial.print("plaintext:");
        Serial.println(plaintext[10], HEX);
    
    
        for (int i = 0 ; i < 16 ; i++)
        {
          uint8_t aes_block [16] ;
          for (int j = 0 ; j < 16 ; j++)
          aes_block [j] = *bptr++ ;
          aes256.setKey(key, aes256.keySize());
          aes256.encryptBlock(buffer, aes_block);
          memcpy(ciphertext + (i*16), buffer,16);
          
        } 
         memcpy(ciphertext2, ciphertext,256);
         Serial.print("ciphertext:");
         Serial.println(ciphertext2[10], HEX);
    
    
    
    
         for (int i = 0 ; i < 16 ; i++)
        {
          uint8_t clear_block [16] ;
          for (int j = 0 ; j < 16 ; j++)
          clear_block [j] = *bptr1++ ;
          aes256.setKey(key, aes256.keySize());
          aes256.decryptBlock(buffer1, clear_block);
          memcpy(plaintext1 + (i*16), buffer1,16);
          
        }
         memcpy(plaintext2, plaintext1,256);
         Serial.print("plaintext:");
         Serial.println(plaintext2[10], HEX);
    
    
    }
    
    void loop()
    {
    
    }
    output is
    Code:
    plaintext:A8E6
    
    ciphertext:CB54
    
    plaintext:A8E6

  5. #30
    Senior Member
    Join Date
    Jul 2020
    Posts
    397
    I suspect there's a uint16_t method for println. Note that int16_t and int32_t will not implicitly cast
    to unsigned types as negative values cannot be represented.

  6. #31
    Quote Originally Posted by MarkT View Post
    I suspect there's a uint16_t method for println. Note that int16_t and int32_t will not implicitly cast
    to unsigned types as negative values cannot be represented.
    audio_block_t data[] is uint16_t so it does not have negative values

  7. #32
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    The audio_block_t data structure is defined in Audiostream.h
    Code:
    typedef struct audio_block_struct {
    	uint8_t  ref_count;
    	uint8_t  reserved1;
    	uint16_t memory_pool_index;
    	int16_t  data[AUDIO_BLOCK_SAMPLES];
    } audio_block_t;
    The data[] array is int16_t.

    Pete

  8. #33
    Senior Member
    Join Date
    Jul 2020
    Posts
    397
    Quote Originally Posted by Nishant View Post
    audio_block_t data[] is uint16_t so it does not have negative values
    You clearly haven't checked this in the source code - I'd recommend getting upto speed with the Audio lib internals
    to gain a deeper understanding of what goes on, AudioStream.h & AudioStream.cpp can be perused here:
    https://github.com/PaulStoffregen/co...master/teensy4

  9. #34
    Quote Originally Posted by MarkT View Post
    You clearly haven't checked this in the source code - I'd recommend getting upto speed with the Audio lib internals
    to gain a deeper understanding of what goes on, AudioStream.h & AudioStream.cpp can be perused here:
    https://github.com/PaulStoffregen/co...master/teensy4
    I am able to copy audio block data for processing , the same works out if I use a pre filled array of similar size

  10. #35
    Quote Originally Posted by Nishant View Post
    I am able to copy audio block data for processing , the same works out if I use a pre filled array of similar size
    Sorry typo error. There seems to be error while typecasting and working with audio_block->data. But when I try my code with static array similar to audio_block->data size my code works

  11. #36
    Quote Originally Posted by el_supremo View Post
    The audio_block_t data structure is defined in Audiostream.h
    Code:
    typedef struct audio_block_struct {
    	uint8_t  ref_count;
    	uint8_t  reserved1;
    	uint16_t memory_pool_index;
    	int16_t  data[AUDIO_BLOCK_SAMPLES];
    } audio_block_t;
    The data[] array is int16_t.

    Pete
    Code:
    audio_block_t *in =NULL;
      in = receiveWritable();
     // if (!in) return;
      Serial.print("Audio_block:");
      Serial.println(in->data[0],HEX);
    I am getting varying values some are 32bits and some are 16bits

    Code:
    Audio_block---:0
    Audio_block---:0
    Audio_block---:0
    Audio_block---:0
    Audio_block---:1
    Audio_block---:6
    Audio_block---:FFFFDDA9
    Audio_block---:FFFFBBC4
    Audio_block---:294C
    Audio_block---:11B8
    Audio_block---:FFFFF48B
    Audio_block---:D2
    Audio_block---:24A
    Audio_block---:1778
    Audio_block---:FFFFC256
    Audio_block---:19A6
    Audio_block---:DF1
    Audio_block---:FFFFEC84
    Audio_block---:F66
    Audio_block---:3CB
    Audio_block---:4BAC
    Audio_block---:FFFFD8DD
    Audio_block---:13D1
    Audio_block---:17FD
    Audio_block---:A8C
    Audio_block---:20F8
    Audio_block---:FFFFFDC0
    Audio_block---:567F
    Audio_block---:FFFFEC0C
    Audio_block---:FFFFE5EB
    Audio_block---:1D8E
    Audio_block---:2
    Audio_block---:81C
    Audio_block---:FFFFFF27
    Audio_block---:620C
    Audio_block---:13BF
    Audio_block---:FFFFE7B2
    Audio_block---:330E
    Audio_block---:6DE
    Audio_block---:FFFFFFA1
    Audio_block---:8D
    Audio_block---:E51
    Audio_block---:3742
    Audio_block---:FFFFE126
    Audio_block---:2DFC
    Audio_block---:5A2
    Audio_block---:FFFFFDF3
    Audio_block---:FFFFFF9B
    Audio_block---:FFFFBD5D
    Audio_block---:466F
    Audio_block---:FFFFD5F8
    Audio_block---:1E42
    Audio_block---:144A
    Audio_block---:20F7
    Audio_block---:1EF2
    Audio_block---:FFFF9E1F
    Audio_block---:5EE2
    Audio_block---:FFFFC007
    Audio_block---:B69
    Audio_block---:FFFFE3FC
    Audio_block---:FFFFDB17
    Audio_block---:71B
    Audio_block---:FFFF9654
    Audio_block---:4CD4
    Audio_block---:FFFFD27D
    Audio_block---:207
    Audio_block---:1557
    Audio_block---:FFFFFBF5
    Audio_block---:FFFFF96F
    Audio_block---:FFFFE7C2
    Audio_block---:3FEE
    Audio_block---:FFFFD427
    Audio_block---:FFFFE99E
    Audio_block---:11B3
    Audio_block---:C20
    Audio_block---:59
    Audio_block---:FFFFF2EC
    Audio_block---:325F
    Audio_block---:FFFFEC27
    Audio_block---:FFFFE20F
    Audio_block---:FFFFFFD5
    Audio_block---:B52
    Audio_block---:D04
    Audio_block---:492D
    Audio_block---:429
    Audio_block---:FFFFEEA9
    Audio_block---:FFFFDBC7
    Audio_block---:E89
    Audio_block---:1E8
    Audio_block---:5E
    Audio_block---:4B6B
    Audio_block---:FFFFEBCE
    Audio_block---:46B
    Audio_block---:FFFFC8E4
    Audio_block---:FFFFFF00
    Audio_block---:FFFFFDB5
    Audio_block---:FFFFF98B
    Audio_block---:4781
    Audio_block---:FFFFC6D4
    Audio_block---:3916
    Audio_block---:FFFFBD11
    Audio_block---:3CF
    Audio_block---:10B6
    Audio_block---:D2B
    Audio_block---:FFFFFFFD
    Audio_block---:FFFFC669
    Audio_block---:27A8
    Audio_block---:FFFFD75A
    Audio_block---:FFFFFADD
    Audio_block---:FFFFE3AB
    Audio_block---:CA3

  12. #37
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    some are 32bits and some are 16bits
    No they aren't. They are all 32-bits. The difference is that all the numbers that begin with FFFF were negative and FFFF is the sign extension which occurs when a negative 16-bit number is converted to a 32-bit number.
    As @MarkT has explained, if you pass a 16-bit signed integer to Serial.print, it is converted to a signed 32-bit integer because that is what Serial.print handles.
    If you want to always get just the low order 4 HEX digits, cast the sample as (uint16_t) which will make the compiler treat the number as if it was an unsigned 16-bit number. This will still be converted to a 32-bit number but the sign extension won't be done because it is now considered to be unsigned.
    For example, in this snippet:
    Code:
      int16_t i16 = -1;
      Serial.println(i16,HEX);
      Serial.println((uint16_t)i16,HEX);
    The first println prints FFFFFFFF because that is the 32-bit representation of the 16-bit negative number -1 (FFFF).
    The second println prints FFFF because the cast (uint16_t) forces the number to be treated as if it were unsigned and so the sign extension is not done and the 32-bit result is 0000FFFF, but println doesn't print leading zeroes so the output is FFFF.

    Pete

  13. #38
    Quote Originally Posted by el_supremo View Post
    No they aren't. They are all 32-bits. The difference is that all the numbers that begin with FFFF were negative and FFFF is the sign extension which occurs when a negative 16-bit number is converted to a 32-bit number.
    As @MarkT has explained, if you pass a 16-bit signed integer to Serial.print, it is converted to a signed 32-bit integer because that is what Serial.print handles.
    If you want to always get just the low order 4 HEX digits, cast the sample as (uint16_t) which will make the compiler treat the number as if it was an unsigned 16-bit number. This will still be converted to a 32-bit number but the sign extension won't be done because it is now considered to be unsigned.
    For example, in this snippet:
    Code:
      int16_t i16 = -1;
      Serial.println(i16,HEX);
      Serial.println((uint16_t)i16,HEX);
    The first println prints FFFFFFFF because that is the 32-bit representation of the 16-bit negative number -1 (FFFF).
    The second println prints FFFF because the cast (uint16_t) forces the number to be treated as if it were unsigned and so the sign extension is not done and the 32-bit result is 0000FFFF, but println doesn't print leading zeroes so the output is FFFF.

    Pete
    Thanks a lot , that made it very clear. I have running the code given below and instead of taking a audio_block , I am testing with a fixes array of [128] and my code works but it fails when a take live audio data

    Code:
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    
    AESSmall256 aes256;
    
    int16_t plaintext[128] = {0xFFFF,
    0x29f7,
    0x5999,
    0x825e,
    0xcc56,
    0x9ca1,
    0xf0e9,
    0x1a12,
    0xf1e7,
    0x52b6,
    0xa8e6,
    0x7233,
    0x7235,
    0x0f94,
    0x690f,
    0x507a,
    0x7cb3,
    0xe651,
    0xd0a7,
    0x6130,
    0x6e8a,
    0xd554,
    0x7ce2,
    0x8ece,
    0x62de,
    0x5e91,
    0xb845,
    0xfcc5,
    0x62d6,
    0xa366,
    0xcb00,
    0x0ea6,
    0x1600,
    0x52b4,
    0x72f9,
    0xece0,
    0xf444,
    0x5c18,
    0x0df5,
    0x0261,
    0xe604,
    0x69dc,
    0x7b9f,
    0xdcf3,
    0x292a,
    0x6305,
    0x21e0,
    0x7c1b,
    0x0dc6,
    0x0fd4,
    0x75f9,
    0xe663,
    0xa6d4,
    0x2957,
    0x07f7,
    0xf706,
    0x9e2a,
    0x8c1b,
    0x190e,
    0x0e23,
    0x3391,
    0xcc30,
    0xc724,
    0xd415,
    0x3d28,
    0x9997,
    0xf8c0,
    0xe66d,
    0x6326,
    0x57cb,
    0x7e2b,
    0x29d4,
    0x2681,
    0xb9d6,
    0xda2f,
    0x4829,
    0x8442,
    0xd043,
    0xeb78,
    0xf121,
    0x0d99,
    0x1a59,
    0x0bc9,
    0xafa7,
    0x5e73,
    0xe362,
    0x5dfc,
    0x0aaa,
    0x4320,
    0x8928,
    0x3a85,
    0xa25a,
    0x5d07,
    0x73f4,
    0xcee6,
    0x8f86,
    0xcf45,
    0x5635,
    0x87dd,
    0xfe41,
    0x52cb,
    0x5418,
    0xf7e7,
    0x8ff6,
    0xd67e,
    0x7692,
    0xfe41,
    0xccd8,
    0x0c29,
    0xbc57,
    0x0910,
    0x00d2,
    0x0c39,
    0xfc2e,
    0xd0ef,
    0x0a52,
    0x09a4,
    0xd577,
    0x05a6,
    0x0199,
    0xd918,
    0x0a79,
    0x0bf9,
    0x04a1,
    0x06fe,
    0x007e,
    0x0516,
    0x01e8};
    
    
    int16_t plaintext2[128];
    int16_t ciphertext2[128];
    int8_t  buffer[16];
    int8_t  buffer1[16];
    
    int8_t ciphertext[256];
    int8_t plaintext1[256];
    int count =0 ;
    
    
    int8_t  key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    int8_t * bptr = (int8_t*) (plaintext);
    int8_t * bptr1 = (int8_t*) (ciphertext2);
    
    void setup()
    {
        
        Serial.begin(9600);
        delay(1000);
        Serial.print("plaintext:");
        Serial.println(plaintext[0], HEX);
    
    
    
        for (int i = 0 ; i < 16 ; i++)
        {
          int8_t aes_block [16] ;
          for (int j = 0 ; j < 16 ; j++)
          aes_block [j] = *bptr++ ;
          aes256.setKey(key, aes256.keySize());
          aes256.encryptBlock(buffer, aes_block);
          memcpy(ciphertext + (i*16), buffer,16);
          
        } 
         memcpy(ciphertext2, ciphertext,256);
         Serial.print("ciphertext:");
         Serial.println(ciphertext2[0], HEX);
    
    
    
    
         for (int i = 0 ; i < 16 ; i++)
        {
          int8_t clear_block [16] ;
          for (int j = 0 ; j < 16 ; j++)
          clear_block [j] = *bptr1++ ;
          aes256.setKey(key, aes256.keySize());
          aes256.decryptBlock(buffer1, clear_block);
          memcpy(plaintext1 + (i*16), buffer1,16);
          
        }
         memcpy(plaintext2, plaintext1,256);
         Serial.print("plaintext:");
         Serial.println(plaintext2[0], HEX);
    
    
    }
    
    void loop()
    {
    
    }
    output
    Code:
    plaintext:FFFFFFFF
    
    ciphertext:5801
    
    plaintext:FFFFFFFF

  14. #39
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    When you print (or println) int16_t data, you must always cast it as (uint16_t) to stop the implicit sign extension. E.g.
    Code:
        Serial.println((uint16_t)plaintext[0], HEX);
    Then it will print this as FFFF

    Pete

  15. #40
    Finally I have been able to make some code. In the code below I am doing the encryption and decryption to together and it works but with some noise.

    encryption + decryption
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioInputUSB            usbin;          //xy=91.33332824707031,214
    AudioRecordQueue         queuein;        //xy=280.33331298828125,336
    AudioPlayQueue           queueout;       //xy=401.33331298828125,223
    AudioOutputI2S           i2s1;           //xy=578.3333129882812,328
    AudioOutputUSB           usbout;         //xy=593.3333282470703,210
    AudioConnection          patchCord1(usbin, 1, queuein, 0);
    AudioConnection          patchCord2(queueout, 0, usbout, 1);
    AudioConnection          patchCord3(queueout, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=298.3333282470703,135
    // GUItool: end automatically generated code
    
    
    AES256 aes256;
    
    byte key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    short array1[AUDIO_BLOCK_SAMPLES];
    short array2[AUDIO_BLOCK_SAMPLES];
    
    
    
    int ret=0;
    int j=0;
    byte buffer [16];
    byte buffer2 [16];
    byte cleartext[256];
    byte ciphertext[256];
    byte cleartext2[256];
    
    void setup() {
      Serial.begin(9600);
      while (!Serial) ;
      delay(1000);
      AudioMemory(64);  
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
      sgtl5000_1.volume(0.5);
      queuein.begin();
    }
    
    void loop() {
      
      ret=queuein.available();
    
      if(ret>=1)
      {
        memcpy(array1, queuein.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
        queuein.freeBuffer();
    //    Serial.print("Audio_block:");
    //    Serial.println(array1[0],HEX);
    
        int i = 0;
        while(i< AUDIO_BLOCK_SAMPLES)
        {
          cleartext[i] = (array1[i] & 0xFF);
          cleartext[i+1] = (array1[i] >> 8) & 0xFF;
          i++;
          i++;
        }
        
        for (int j = 0 ; j < 16 ; j++)
        {
        byte aes_block [16];
        memcpy(aes_block, cleartext+(j*16),16);
        aes256.setKey(key, aes256.keySize());
        aes256.encryptBlock(buffer, aes_block);
        memcpy(ciphertext + (j*16), buffer,16);
        }
        
    //    Serial.print("cipher:");
     //   Serial.print(ciphertext[0],HEX);
    //    Serial.println(ciphertext[1],HEX);
        
        for (int l = 0 ; l < 16 ; l++)
        {
        byte aes_block2 [16];
        memcpy(aes_block2, ciphertext+(l*16),16);
        aes256.setKey(key, aes256.keySize());
        aes256.decryptBlock(buffer2, aes_block2);
        memcpy(cleartext2 + (l*16), buffer2,16);
        }
        
     //   Serial.print("clear:");
     //   Serial.print(cleartext2[0],HEX);
      //  Serial.println(cleartext2[1],HEX);
    
      //  byte low = (array1[0] & 0xFF);
      //  byte high = (array1[0] >> 8) & 0xFF;
    
        
        memcpy(array2, cleartext2,256);
     //   Serial.print("clear_Audio_block---:");
     //   Serial.println(array2[0],HEX);
     
        memcpy(queueout.getBuffer(), array2, 2*AUDIO_BLOCK_SAMPLES);
        queueout.playBuffer();
     
      }
    
    }
    However when I try to make encryption and decryption code separately and first run encryption and then decryption, I don't get the the original audio back. I am not able to zero on the error. please help

    Encryption Code:-
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioInputUSB            usbin;          //xy=91.33332824707031,214
    AudioRecordQueue         queuein;        //xy=280.33331298828125,336
    AudioPlayQueue           queueout;       //xy=401.33331298828125,223
    AudioOutputI2S           i2s1;           //xy=578.3333129882812,328
    AudioOutputUSB           usbout;         //xy=593.3333282470703,210
    AudioConnection          patchCord1(usbin, 1, queuein, 0);
    AudioConnection          patchCord2(queueout, 0, usbout, 1);
    AudioConnection          patchCord3(queueout, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=298.3333282470703,135
    // GUItool: end automatically generated code
    
    
    AESSmall256 aes256;
    
    byte key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    short array1[AUDIO_BLOCK_SAMPLES];
    short array2[AUDIO_BLOCK_SAMPLES];
    
    
    
    int ret=0;
    int j=0;
    byte buffer [16];
    byte buffer2 [16];
    byte cleartext[256];
    byte ciphertext[256];
    byte cleartext2[256];
    
    void setup() {
      Serial.begin(9600);
      while (!Serial) ;
      delay(1000);
      AudioMemory(64);  
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
      sgtl5000_1.volume(0.5);
      queuein.begin();
    }
    
    void loop() {
      
      ret=queuein.available();
    
      if(ret>=1)
      {
        memcpy(array1, queuein.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
        queuein.freeBuffer();
        Serial.print("Audio_block:");
        Serial.println(array1[0],HEX);
    
        int i = 0;
        while(i< AUDIO_BLOCK_SAMPLES)
        {
          cleartext[i] = (array1[i] & 0xFF);
          cleartext[i+1] = (array1[i] >> 8) & 0xFF;
          i++;
          i++;
        }
        
        for (int j = 0 ; j < 16 ; j++)
        {
        byte aes_block [16];
        memcpy(aes_block, cleartext+(j*16),16);
        aes256.setKey(key, aes256.keySize());
        aes256.encryptBlock(buffer, aes_block);
        memcpy(ciphertext + (j*16), buffer,16);
        }
        
    
        memcpy(array2, ciphertext,256);
        Serial.print("cipher_Audio_block---:");
        Serial.println(array2[0],HEX);
     
        memcpy(queueout.getBuffer(), array2, 2*AUDIO_BLOCK_SAMPLES);
        queueout.playBuffer();
     
      }
    
    }
    Decryption code :
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Crypto.h>
    #include <AES.h>
    #include <string.h>
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioInputUSB            usbin;          //xy=91.33332824707031,214
    AudioRecordQueue         queuein;        //xy=280.33331298828125,336
    AudioPlayQueue           queueout;       //xy=401.33331298828125,223
    AudioOutputI2S           i2s1;           //xy=578.3333129882812,328
    AudioOutputUSB           usbout;         //xy=593.3333282470703,210
    AudioConnection          patchCord1(usbin, 1, queuein, 0);
    AudioConnection          patchCord2(queueout, 0, usbout, 1);
    AudioConnection          patchCord3(queueout, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=298.3333282470703,135
    // GUItool: end automatically generated code
    
    
    AESSmall256 aes256;
    
    byte key[32] ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    
    short array1[AUDIO_BLOCK_SAMPLES];
    short array2[AUDIO_BLOCK_SAMPLES];
    
    
    
    int ret=0;
    int j=0;
    byte buffer [16];
    byte buffer2 [16];
    byte cleartext[256];
    byte ciphertext[256];
    byte cleartext2[256];
    
    void setup() {
      Serial.begin(9600);
      while (!Serial) ;
      delay(1000);
      AudioMemory(64);  
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
      sgtl5000_1.volume(0.5);
      queuein.begin();
    }
    
    void loop() {
      
      ret=queuein.available();
    
      if(ret>=1)
      {
        memcpy(array1, queuein.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
        queuein.freeBuffer();
        Serial.print("Audio_block:");
        Serial.println(array1[0],HEX);
    
        int i = 0;
        while(i< AUDIO_BLOCK_SAMPLES)
        {
          cleartext[i] = (array1[i] & 0xFF);
          cleartext[i+1] = (array1[i] >> 8) & 0xFF;
          i++;
          i++;
        }
        
        for (int j = 0 ; j < 16 ; j++)
        {
        byte aes_block [16];
        memcpy(aes_block, cleartext+(j*16),16);
        aes256.setKey(key, aes256.keySize());
        aes256.decryptBlock(buffer, aes_block);
        memcpy(ciphertext + (j*16), buffer,16);
        }
        
        memcpy(array2, ciphertext,256);
        Serial.print("clear_Audio_block---:");
        Serial.println(array2[0],HEX);
     
        memcpy(queueout.getBuffer(), array2, 2*AUDIO_BLOCK_SAMPLES);
        queueout.playBuffer();
     
      }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •