int main(int argc, char** argv) { double d; int n = 1; if( argc!=2) { printf( "usage: program <number of numbers to read>\n"); return -1; } #ifdef STDIO FILE *f; f = fopen( "numbers.txt", "r"); if ( !f) { printf( "Cannot open file\n"); return -1; } VALUES = atoi( argv[1]); for ( ; VALUES; --VALUES) { fscanf( f,"%lf", &d); printf( "%10.5f", d); #else ifstream f( "numbers.txt", ios_base::in); if( !f.is_open()) { cout << "Cannot open file\n"; return -1; } VALUES = atoi( argv[1]); for ( ; VALUES; --VALUES) { f >> d; cout << std::setw(10) << std::setprecision(5) << std::setiosflags( ios::showpoint) << std::setiosflags( ios::fixed) << d; #endif if (n % 5 == 0) { #ifdef STDIO printf("\n"); #else cout << '\n'; #endif } } #ifdef STDIO fclose( f); #else f.close(); #endif return (EXIT_SUCCESS); }
void *connection_handler(void *socket_desc) { FILE *file; int sock = *(int*) socket_desc; int read_size; char *message, client_message[8192]; OggStream* stream = 0; StreamMap streams; // start opus int error; OpusDecoder *dec; dec = opus_decoder_create(16000, 1, &error); if (error != OPUS_OK && dec == NULL) { puts("ERROR LOADING OPUS"); } // start pocketsphinx ps_decoder_t *ps = ps_start(); /* // IF RUN VAD AT SERVER VadInst* handle = NULL; if (WebRtcVad_Create(&handle) < 0) puts("Error creating WebRtcVad_Create"); if (WebRtcVad_Init(handle) < 0) puts("Error initializing WebRtcVad_Init"); if (WebRtcVad_set_mode(handle, 3) < 0) puts("Error setting mode WebRtcVad_set_mode"); */ char * dtdepois; bool ended = true; while ((read_size = recv(sock, client_message, 8192, 0)) > 0) { char otherString[3]; strncpy(otherString, client_message, 3); if (strcmp(otherString, "#JS") == 0) { puts("GRAM RECVD. SWITCH"); string str1(client_message); char *dtgram = current_timestamp(); std::string grampath = "/var/www/speechrtc/voiceserver/" + std::string(dtgram) + ".jsgf"; ofstream file; file.open(grampath.c_str()); file << str1; file.close(); jsgf_rule_iter_t *itor; jsgf_t * gram = jsgf_parse_file(grampath.c_str(), NULL); jsgf_rule_t * rule; for (itor = jsgf_rule_iter(gram); itor; itor = jsgf_rule_iter_next(itor)) { rule = jsgf_rule_iter_rule(itor); if (jsgf_rule_public(rule)) break; } fsg_model_t * m = jsgf_build_fsg(gram, rule, ps_get_logmath(ps), 6.5); fsg_set_t* fsgset = ps_get_fsgset(ps); fsg_set_add(fsgset, dtgram , m); fsg_set_select(fsgset , dtgram ); ps_update_fsgset(ps); continue; } if (strcmp(otherString, "STA") == 0) { // int _res = ps_start_utt(ps, "goforward"); dtdepois = current_timestamp(); puts(dtdepois); file = fopen(dtdepois, "wb+"); ended = false; continue; } if (strcmp(otherString, "END") == 0) { puts("END RECVD"); fclose(file); FILE *_file = fopen(dtdepois, "rb"); char const *hyp, *uttid; int32 score; int rv = ps_decode_raw(ps, _file, dtdepois, -1); if (rv < 0) puts("error ps_decode_raw"); hyp = ps_get_hyp(ps, &score, &uttid); if (hyp == NULL) { puts("Error hyp()"); write(sock, "ERR", strlen("ERR")); } else { printf("Recognized: %s\n", hyp); // envia final hypothesis write(sock, hyp, strlen(hyp)); } fclose(_file); ended = true; continue; } if (ended) { puts("rcv packet after end. ignoring"); continue; } // decode ogg ogg_sync_state state; ogg_page page; int ret = ogg_sync_init(&state); char *buffer = ogg_sync_buffer(&state, 8192); memcpy(buffer, client_message, 8192); ret = ogg_sync_wrote(&state, 8192); // here we accumulate the decoded pcm int totalpcm = 0; while (ogg_sync_pageout(&state, &page) == 1) { int serial = ogg_page_serialno(&page); if (ogg_page_bos(&page)) { stream = new OggStream(serial); ret = ogg_stream_init(&stream->mState, serial); streams[serial] = stream; } else { stream = streams[serial]; } ret = ogg_stream_pagein(&stream->mState, &page); ogg_packet packet; while (ret = ogg_stream_packetout(&stream->mState, &packet)) { if (ret == 0) { // Need more data to be able to complete the packet puts("Need more data to be able to complete the packet"); continue; } else if (ret == -1) { puts("// We are out of sync and there is a gap in the data. We lost a page somewhere."); continue; } // A packet is available, this is what we pass to opus stream->mPacketCount++; //puts("OGG OK"); long length_pack = packet.bytes; unsigned char * data_pack = packet.packet; // decode opus int frame_size = 1920; int lenaudio = frame_size * 1 * sizeof (opus_int16); //opus_int16 audio[lenaudio]; short *in, *pcmsamples; pcmsamples = (short*) malloc(frame_size * 1 * sizeof (short)); int totalpcm = opus_decode(dec, data_pack, length_pack, pcmsamples, frame_size, 0); // treat for more errors if (OPUS_INTERNAL_ERROR == totalpcm) puts("OPUS_INTERNAL_ERROR"); else if (OPUS_INVALID_PACKET == totalpcm) { puts("OPUS_INVALID_PACKET"); } else { //puts("OPUS OK. Sending PS"); puts("written to file"); fwrite(pcmsamples, 2, totalpcm, file); //ps_process_raw(ps, pcmsamples, totalpcm, false, false); // envia partial hypothesis to node.js send to browser // write(sock , client_message , strlen(client_message)); } } } memset(client_message, 0, 8192); } if (read_size == 0) { // EVERYTHING CLEANED AT READ ERROR puts("Client disconnected"); fflush(stdout); // ps_free(ps); } else if (read_size == -1) { perror("recv failed"); } puts("ending handler"); // ps_free(ps); return 0; }
int ACE_TMAIN (int argcw, ACE_TCHAR *argvw[]) { CORBA::ORB_var orb_var; try { Catior_i catior_impl; orb_var = CORBA::ORB_init (argcw, argvw); CORBA::Boolean b = false; CORBA::Boolean have_argument = false; int opt; ACE_Get_Opt get_opt (argcw, argvw, ACE_TEXT ("f:n:x")); while ((opt = get_opt ()) != EOF) { // some arguments have been supplied have_argument = 1; switch (opt) { case 'n': { // Read the CosName from the NamingService convert the // object_ptr to a CORBA::String_var via the call to // object_to_string. ACE_DEBUG ((LM_DEBUG, "opening a connection to the NamingService\n" "resolving the CosName %s\n", get_opt.opt_arg ())); CORBA::Object_var server_object; try { // Find the Naming Service. CORBA::Object_var naming_context_object = orb_var->resolve_initial_references ("NameService"); CosNaming::NamingContextExt_var naming_context = CosNaming::NamingContextExt::_narrow (naming_context_object.in ()); if (CORBA::is_nil (naming_context.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "NameService cannot be resolved\n"), -1); } CosNaming::Name *name = naming_context->to_name (ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg ())); try { server_object = naming_context->resolve (*name); if (CORBA::is_nil (server_object.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "name is not resolved to a valid object\n"), -1); } } catch (const CosNaming::NamingContext::NotFound& nf) { const ACE_TCHAR *reason; switch (nf.why) { case CosNaming::NamingContext::missing_node: reason = ACE_TEXT ("missing node"); break; case CosNaming::NamingContext::not_context: reason = ACE_TEXT ("not context"); break; case CosNaming::NamingContext::not_object: reason = ACE_TEXT ("not object"); break; default: reason = ACE_TEXT ("not known"); break; } ACE_ERROR_RETURN ((LM_ERROR, "%s cannot be resolved, exception reason = %s\n", get_opt.opt_arg (), reason), -1); } catch (const CosNaming::NamingContext::InvalidName&) { ACE_ERROR_RETURN ((LM_ERROR, "%s cannot be resolved, exception reason = " "Invalid Name" "\n", get_opt.opt_arg ()), -1); } catch (const CosNaming::NamingContext::CannotProceed&) { ACE_ERROR_RETURN ((LM_ERROR, "%s cannot be resolved, exception reason = " "Cannot Proceed" "\n", get_opt.opt_arg ()), -1); } catch (const CORBA::Exception&) { ACE_ERROR_RETURN ((LM_ERROR, "%s cannot be resolved, exception reason = " "Unexpected Exception" "\n", argvw[0]), -1); } ACE_CString aString; aString = orb_var->object_to_string (server_object.in ()); ACE_DEBUG ((LM_DEBUG, "\nhere is the IOR\n%C\n\n", aString.rep ())); ACE_CString str; b = catior_impl.decode(aString, str); ACE_DEBUG ((LM_DEBUG, "%s", str.c_str())); } catch (const CORBA::Exception&) { ACE_ERROR_RETURN ((LM_ERROR, "%s cannot be resolved, exception reason = " "Unexpected Exception" "\n", argvw[0]), -1); } if (b == 1) ACE_DEBUG ((LM_DEBUG, "catior returned true\n")); else ACE_DEBUG ((LM_DEBUG, "catior returned false\n")); break; } case 'f': { int have_some_input = 0; int decode_pass_count = 0; // Read the file into a CORBA::String_var. ACE_DEBUG ((LM_DEBUG, "reading the file %s\n", get_opt.opt_arg ())); #if !defined (ACE_LACKS_IOSTREAM_TOTALLY) ifstream ifstr (ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ())); if (!ifstr.good ()) { ifstr.close (); ACE_ERROR_RETURN ((LM_ERROR, "%s " "-f %s " "\n" "Invalid IOR file nominated" "\n", argvw[0], get_opt.opt_arg ()), -1); } while (!ifstr.eof()) { ACE_CString aString; have_some_input = 0; while (!ifstr.eof ()) { char ch = 0; ifstr.get (ch); if (ifstr.eof () || ch == '\n' || ch == '\r') break; aString += ch; ++have_some_input; } #else FILE* ifstr = ACE_OS::fopen (get_opt.opt_arg (), ACE_TEXT ("r")); if (!ifstr || ferror (ifstr)) { if (ifstr) { ACE_OS::fclose (ifstr); } ACE_ERROR_RETURN ((LM_ERROR, "%s " "-f %s " "\n" "Invalid IOR file nominated" "\n", argvw[0], get_opt.opt_arg ()), -1); } while (!feof (ifstr)) { char ch; ACE_CString aString; have_some_input = 0; while (!feof (ifstr)) { ch = ACE_OS::fgetc (ifstr); if (ch == EOF || ch == '\n' || ch == '\r') break; aString += ch; ++have_some_input; } #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */ if (have_some_input == 0 || !aString.length()) { if (!decode_pass_count) { ACE_ERROR_RETURN ((LM_ERROR, "%s " "-f %s " "\n" "Empty IOR file nominated" "\n", argvw[0], get_opt.opt_arg ()), -1); } else { ACE_DEBUG ((LM_DEBUG, "catior returned true\n")); return 0; // All done now } } ++decode_pass_count; ACE_DEBUG ((LM_DEBUG, "\nhere is the IOR\n%C\n\n", aString.rep ())); ACE_CString str; b = catior_impl.decode(aString, str); ACE_DEBUG ((LM_DEBUG, "%s", str.c_str())); } if (b == 1) ACE_DEBUG ((LM_DEBUG, "catior returned true\n")); else ACE_DEBUG ((LM_DEBUG, "catior returned false\n")); #if !defined (ACE_LACKS_IOSTREAM_TOTALLY) ifstr.close (); #else ACE_OS::fclose (ifstr); #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */ } break; case 'x': { int have_some_input = 0; int decode_pass_count = 0; // Read the input into a CORBA::String_var. ACE_DEBUG ((LM_DEBUG, "reading from stdin\n")); #if !defined (ACE_LACKS_IOSTREAM_TOTALLY) if (!cin.good ()) { ACE_ERROR_RETURN ((LM_ERROR, "%s " "-x" "\n" "Invalid input stream" "\n", argvw[0]), -1); } while (!cin.eof()) { ACE_CString aString; have_some_input = 0; while (!cin.eof ()) { char ch = 0; cin.get (ch); if (cin.eof () || ch == '\n' || ch == '\r') break; aString += ch; ++have_some_input; } #else FILE* ifstr = stdin; if (!ifstr || ferror (ifstr)) { if (ifstr) { ACE_OS::fclose (ifstr); } ACE_ERROR_RETURN ((LM_ERROR, "%s " "-x" "\n" "Invalid input stream" "\n", argvw[0]), -1); } while (!feof (ifstr)) { char ch; ACE_CString aString; have_some_input = 0; while (!feof (ifstr)) { ch = ACE_OS::fgetc (ifstr); if (ch == EOF || ch == '\n' || ch == '\r') break; aString += ch; ++have_some_input; } #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */ if (have_some_input == 0) { if (!decode_pass_count) { ACE_ERROR_RETURN ((LM_ERROR, "%s " "-x" "\n" "Empty input stream" "\n", argvw[0]), -1); } else { return 0; // All done now } } ++decode_pass_count; ACE_DEBUG ((LM_DEBUG, "\nhere is the IOR\n%C\n\n", aString.rep ())); ACE_CString str; b = catior_impl.decode(aString, str); ACE_DEBUG ((LM_DEBUG, "%s", str.c_str())); } if (b == 1) ACE_DEBUG ((LM_DEBUG, "catior returned true\n")); else ACE_DEBUG ((LM_DEBUG, "catior returned false\n")); } break; case '?': case 'h': default: ACE_ERROR_RETURN ((LM_ERROR, "Usage: %s " "-f filename " "-n CosName " "\n" "Reads an IOR " "and dumps the contents to stdout " "\n", argvw[0]), 1); } } // check that some relevant arguments have been supplied if (have_argument == 0) ACE_ERROR_RETURN ((LM_ERROR, "Usage: %s " "-f filename " "-n CosName " "\n" "Reads an IOR " "and dumps the contents to stdout " "\n", argvw[0]), 1); } catch (const CORBA::Exception& ex) { ACE_DEBUG ((LM_DEBUG, "\nError:\n")); ex._tao_print_exception ("Exception in nsadd"); orb_var->destroy (); return 1; } return 0; }