Last active
February 7, 2018 16:12
-
-
Save kdpatino/e5f396b69b5aea8cfffb8431ba7a00f6 to your computer and use it in GitHub Desktop.
DAC test in MATRIX Voice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 <Admobilize> | |
* All rights reserved. | |
*/ | |
#include <gflags/gflags.h> | |
#include <wiringPi.h> | |
#include <math.h> | |
#include <fstream> | |
#include <iostream> | |
#include <string> | |
#include <thread> | |
#include <valarray> | |
#include "../cpp/driver/everloop.h" | |
#include "../cpp/driver/everloop_image.h" | |
#include "../cpp/driver/microphone_array.h" | |
#include "../cpp/driver/wishbone_bus.h" | |
DEFINE_int32(sampling_frequency, 44100, "Sampling Frequency"); | |
DEFINE_int32(duration, 1, "Interrupt after N seconds"); | |
namespace hal = matrix_hal; | |
int main(int argc, char *agrv[]) { | |
google::ParseCommandLineFlags(&argc, &agrv, true); | |
hal::WishboneBus bus; | |
bus.SpiInit(); | |
int sampling_rate = FLAGS_sampling_frequency; | |
int seconds_to_record = FLAGS_duration; | |
std::cout << "Duration : " << seconds_to_record << "s" << std::endl; | |
std::thread et( | |
[seconds_to_record, sampling_rate](hal::WishboneBus *bus) { | |
std::ifstream is("/home/pi/song.raw"); | |
int delta, t; | |
int l = 1024; | |
uint16_t test[l]; | |
float fs = float(sampling_rate); | |
float ts = 1.0 / fs; | |
float f = 1000; | |
float w = 2 * M_PI * f; | |
float A = (pow(2, 16)) - 1; | |
t=0; | |
while (true) { | |
delta = (t) * l; | |
// for (int i = 0; i < l; i++) { | |
// test[i] = uint16_t(A/2 + A*sin(w*(delta+i)*ts)/128); | |
// } | |
is.read((char *)test,l*2); | |
uint16_t write_pointer; | |
uint16_t read_pointer; | |
uint16_t diff; | |
bus->SpiRead16(0x6802,(unsigned char*)&read_pointer); | |
bus->SpiRead16(0x6803,(unsigned char*)&write_pointer); | |
if(write_pointer > read_pointer) | |
diff = write_pointer - read_pointer; | |
else | |
diff = 4096 - read_pointer + write_pointer; | |
if(diff > 3072){ | |
int sleep = int(l*ts*1000000); | |
std::this_thread::sleep_for(std::chrono::microseconds(sleep)); | |
} | |
bus->SpiWriteBurst(0x6000, | |
reinterpret_cast<unsigned char *>(&test[0]), | |
sizeof(uint16_t) * l); | |
t++; | |
} | |
}, | |
&bus); | |
et.join(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment