Created
September 29, 2016 21:57
-
-
Save anonymous/61262807963dbbced9e02ce6c1aa8813 to your computer and use it in GitHub Desktop.
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
/* | |
____ _____ _ _ | |
| __ )| ____| | / \ | |
| _ \| _| | | / _ \ | |
| |_) | |___| |___ / ___ \ | |
|____/|_____|_____/_/ \_\ | |
The platform for ultra-low latency audio and sensor processing | |
http://bela.io | |
A project of the Augmented Instruments Laboratory within the | |
Centre for Digital Music at Queen Mary University of London. | |
http://www.eecs.qmul.ac.uk/~andrewm | |
(c) 2016 Augmented Instruments Laboratory: Andrew McPherson, | |
Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, | |
Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. | |
The Bela software is distributed under the GNU Lesser General Public License | |
(LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt | |
*/ | |
#include <Bela.h> | |
#include <cmath> | |
//janMod | |
#include <stdio.h> | |
#include <fcntl.h> /* File Control Definitions */ | |
#include <termios.h> /* POSIX Terminal Control Definitions */ | |
#include <unistd.h> /* UNIX Standard Definitions */ | |
#include <errno.h> /* ERROR Number Definitions */ | |
//typedef enum { false, true } bool; | |
float gFrequency = 440.0; | |
float gPhase; | |
float gInverseSampleRate; | |
//janMod/* | |
int fd;/*File Descriptor*/ | |
printf("\n +----------------------------------+"); | |
printf("\n | Serial Port Read |"); | |
printf("\n +----------------------------------+"); | |
/*------------------------------- Opening the Serial Port -------------------------------*/ | |
/* Change /dev/ttyUSB0 to the one corresponding to your system */ | |
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */ | |
/* O_RDWR - Read/Write access to serial port */ | |
/* O_NOCTTY - No terminal will control the process */ | |
/* Open in blocking mode,read will wait */ | |
if(fd == -1) /* Error Checking */ | |
printf("\n Error! in Opening ttyUSB0 "); | |
else | |
printf("\n ttyUSB0 Opened Successfully "); | |
/*---------- Setting the Attributes of the serial port using termios structure --------- */ | |
struct termios SerialPortSettings; /* Create the structure */ | |
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */ | |
/* Setting the Baud rate */ | |
cfsetispeed(&SerialPortSettings,B9600); /* Set Read Speed as 9600 */ | |
cfsetospeed(&SerialPortSettings,B9600); /* Set Write Speed as 9600 */ | |
/* 8N1 Mode */ | |
SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity Enable bit(PARENB),So No Parity */ | |
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */ | |
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */ | |
SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */ | |
SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */ | |
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */ | |
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */ | |
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */ | |
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/ | |
/* Setting Time outs */ | |
SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */ | |
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */ | |
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/ | |
printf("\n ERROR ! in Setting attributes"); | |
else | |
printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none"); | |
/*------------------------------- Read data from serial port -----------------------------*/ | |
tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */ | |
char read_buffer[1024]; /* Buffer to store the data received */ | |
int bytes_read = 0; /* Number of bytes read by the read() system call */ | |
int i = 0; | |
char c; | |
int numChars = 32; | |
char receivedChars[numChars]; | |
int ndx = 0; | |
char endMarker = '\n'; | |
char rc; | |
bool newData = false; | |
//janMod*/ | |
bool setup(BelaContext *context, void *userData) | |
{ | |
gInverseSampleRate = 1.0 / context->audioSampleRate; | |
gPhase = 0.0; | |
return true; | |
} | |
void render(BelaContext *context, void *userData) | |
{ | |
for(unsigned int n = 0; n < context->audioFrames; n++) { | |
float out = 0.8 * sinf(gPhase); | |
gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; | |
if(gPhase > 2.0 * M_PI) | |
gPhase -= 2.0 * M_PI; | |
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { | |
// Two equivalent ways to write this code | |
// The long way, using the buffers directly: | |
// context->audioOut[n * context->audioOutChannels + channel] = out; | |
// Or using the macros: | |
audioWrite(context, n, channel, out); | |
} | |
} | |
} | |
void cleanup(BelaContext *context, void *userData) | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment