Skip to content

Commit

Permalink
added a read wav file feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jopedroliveira committed Oct 9, 2017
1 parent 0c5e6d2 commit 897e6cd
Showing 1 changed file with 20 additions and 92 deletions.
112 changes: 20 additions & 92 deletions examples/C++/demo2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,46 @@
#define model_filename "resources/snowboy.umdl"
#define sensitivity_str "0.5"

struct userdata{
short * buffer;
int writeindex;
int chunkksizebytes;
};


static int PaRecordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void * duserdata) {
userdata * dstruct = (userdata *) duserdata;
short * buffer_aux = dstruct->buffer;
memcpy(&buffer_aux[dstruct->writeindex*framesPerBuffer], inputBuffer,
dstruct->chunkksizebytes); //buffer is of samples
dstruct->writeindex++;
return paContinue;
}



int main(int argc, char * argv[]) {
std::string usage =
"C++ demo that shows how to use snowboy. In this examle\n"
"User can acquire audio either from a file either fom the mic using PA\n"
"User can read the audio data from a file.\n"
"Atention reading from a file: this software is for simulation/test only,"
"You need to take precautions when loading a file into the memory.\n"
"To run the example:\n\n./demo [time to record] [filename.raw]\n\n"
"If you just want to use a file, you can use:\n\n./demo - file.raw\n\n"
"If you just want to use a file, you can use:\n\n./demo file.raw\n\n"
"IMPORTANT NOTE: Raw file must be 16kHz sample, mono and 16bit";

//default
int recordtime; // seconds
char * filename;

if(argc == 1){
int fsize;
short * data_buffer = NULL;
bool isRaw;
if(argc > 2 || argc < 1){
std::cout << usage << std::endl;
exit(1);
}

if(argc > 1)
recordtime = atoi(argv[1]);
filename = argv[1]; //input: file. else, input: mic

std::string str = filename;
std::string type = ".wav";
if (str.find(type) != std::string::npos) {
std::cout << "found!" << '\n';
}


if(argc > 2)
filename = argv[2]; //input: file. else, input: mic

double chunk_duration = 0.1; //1600 frames per buffer
int channels = 1;
int sample_rate = 16000;
short * data_buffer = NULL;
int fsize = 0;

if(filename != NULL){
FILE * f = fopen(filename,"rb");

if( f == NULL ) {
perror ("Error opening file");
return(-1);
}



// File size
fseek(f,0,SEEK_END);
Expand All @@ -72,66 +55,11 @@ int main(int argc, char * argv[]) {

// Consume all the audio to the buffer
data_buffer = (short *)malloc(fsize);
int aa = fread(&data_buffer[0], 1 ,fsize, f);
printf("Read bytes: %d\n",aa);
int aa = fread(&data_buffer[0], 1 ,fsize, f);
printf("Read bytes: %d\n",aa);
fclose(f);


} else {

unsigned long Frames_Per_Buffer = (unsigned long) sample_rate * chunk_duration;
int chunkksizebytes = Frames_Per_Buffer * sizeof(short) * channels;
fsize = recordtime/chunk_duration*chunkksizebytes;
data_buffer = (short *)malloc(fsize);

userdata * dstruct = new userdata();
dstruct->buffer = data_buffer;
dstruct->chunkksizebytes = chunkksizebytes;
dstruct->writeindex = 0;

PaStreamParameters inputParameters;
PaStream * stream;
PaError err = paNoError;

err = Pa_Initialize(); // Initializing PortAudio API
if (err != paNoError) {
perror("Error initializing portaudio");
exit(-1);
}

inputParameters.device = Pa_GetDefaultInputDevice(); // default input device
inputParameters.channelCount = channels;
inputParameters.sampleFormat = paInt16;
inputParameters.suggestedLatency =
Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;

err = Pa_OpenStream(&stream, &inputParameters, NULL, sample_rate,
Frames_Per_Buffer, paClipOff, PaRecordCallback, dstruct);
if (err != paNoError) {
perror("Unable to initialize stream");
exit(-1);
}


err = Pa_StartStream(stream);
if (err != paNoError) {
perror((char *)"Unable to start stream\n");
exit(-1);
}
printf("Portaudio initialized\n");

int timer = 0;
while (Pa_IsStreamActive(stream) == 1 && timer < recordtime) {
printf(".\n");
Pa_Sleep(1010);
timer++;
}

Pa_Terminate();

}

}

// Initializes Snowboy detector.
snowboy::SnowboyDetect detector(resource_filename, model_filename);
Expand Down

0 comments on commit 897e6cd

Please sign in to comment.