Thanks robsoles, that is helpful. I changed the byte size to 256.
I could not find if the 16 bit of the audio is unsigned or signed. From your response it seems to be signed. With this I changed my code and the output makes much more sense now.
Code:
// Record sound as raw data to a SD card, and play it back.
//
// Requires the audio shield:
// http://www.pjrc.com/store/teensy3_audio.html
//
// This example code is in the public domain.
#include "Bounce.h"
#include "Audio.h"
#include "Wire.h"
#include "SPI.h"
#include "SD.h"
// GUItool: begin automatically generated code
AudioInputAnalog adc1;
AudioRecordQueue queue1;
AudioConnection patchCord1(adc1, queue1);
float y[4411];
const int nbyte = 256;
void setup() {
// Audio connections require memory, and the record queue
// uses this memory to buffer incoming audio.
AudioMemory(60);
Serial.begin(9600);
delay(1000);
Serial.println("STARTING");
}
void loop() {
Serial.println("\nNEW RECORDING CYCLE");
queue1.begin();
elapsedMicros rectime;
while (rectime < 100000) {
}
queue1.end();
Serial.println(rectime);
int npac = queue1.available();
Serial.println(npac);
if (queue1.available() >= 2) {
byte buffer[512];
memcpy(buffer, queue1.readBuffer(), 256);
queue1.freeBuffer();
memcpy(buffer+256, queue1.readBuffer(), 256);
queue1.freeBuffer();
//Serial.println();
for (int k1=0; k1<16-1; k1+=2) {
Serial.print(buffer[k1]);
Serial.print(" ");
Serial.print(buffer[k1+1]);
Serial.print(" | ");
//Serial.print(buffer[k1+1]);
}
Serial.println();
Serial.println("BIG ENDIAN: ");
Serial.print("16 bit int: ");
for (int k1=0; k1<512-1; k1+=2) {
Serial.print(256*buffer[k1] + buffer[k1+1]);
Serial.print(" | ");
}
Serial.println();
Serial.print("2s complement: ");
for (int k1=0; k1<512-1; k1+=2) {
long val = 256*buffer[k1] + buffer[k1+1];
if (val > 32767) {
val = - (65536 - val);
}
Serial.print(val);
Serial.print(" | ");
}
Serial.println();
Serial.println("LITTLE ENDIAN: ");
Serial.print("16 bit int: ");
for (int k1=0; k1<512-1; k1+=2) {
Serial.print(256*buffer[k1+1] + buffer[k1]);
Serial.print(" | ");
}
Serial.println();
Serial.print("2s complement: ");
for (int k1=0; k1<512-1; k1+=2) {
long val = 256*buffer[k1+1] + buffer[k1];
if (val > 32767) {
val = - (65536 - val);
}
Serial.print(val);
Serial.print(" | ");
}
Serial.println();
queue1.clear();
}
delay(15*1000);
}
The output is now as such with the input connected to AGND, and the little endian numbers make sense to me
NEW RECORDING CYCLE
100000
35
3 0 | 6 0 | 251 255 | 4 0 | 16 0 | 4 0 | 13 0 | 0 0 |
BIG ENDIAN:
16 bit int: 768 | 1536 | 64511 | 1024 | 4096 | 1024 | 3328 | 0 |
2s complement: 768 | 1536 | -1025 | 1024 | 4096 | 1024 | 3328 | 0 |
LITTLE ENDIAN:
16 bit int: 3 | 6 | 65531 | 4 | 16 | 4 | 13 | 0 |
2s complement: 3 | 6 | -5 | 4 | 16 | 4 | 13 | 0 |