예제 #1
0
파일: zpp.cpp 프로젝트: jlefley/libzpp
// ctor: input is filename to open.
// only std::ios::in is supported currently.
// if _makeGlobal == TRUE, then the ZipFile is added to the list
// of global zip files.
zppZipArchive::zppZipArchive(const std::string &_fn, std::ios::openmode _mode, bool _makeGlobal)
{
	ourFile = false;
	zipName = _fn;
	canonizePath(zipName);
#ifdef ZPP_INCLUDE_CRYPT
	passwd = NULL;
#endif
	if (_mode & std::ios_base::in) {
		// make sure "binary" mode is selected.
		_mode |= std::ios_base::binary;
#ifdef ZPP_USE_STDIO
                FILE *infile = fopen(zipName.c_str(),"rb");
	        current_pos_v=0;
#else
		std::ifstream *infile = new std::ifstream();
		infile->open(zipName.c_str(),_mode);
#endif
		ourFile = true;
#ifdef ZPP_USE_STDIO
		if (!infile)
			throw zppError("can't open zip file");
#else
		if (infile->fail())
			throw zppError("can't open zip file");
#endif

		str = infile;
	} else {
		throw ("zppZipArchive:: only std::ios::in currently supported.");
	}

	priority = defaultPriority;
	isGlobal = _makeGlobal;

	// build internal data structures
	parseZipDirectory();
}
예제 #2
0
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;
}
예제 #3
0
int main (int argc, char *argv[])
{
    const int n = (1 < argc) ? atoi(argv[1]) : 1000000;  // number of
                                                           // iterations

#if defined( STDIO )
    FILE * target;
    target = stdout;
    if (2 < argc) {  // place output in file
        target = fopen( argv[2], "w" );
    }
#else                                         // for both iostreams libs
    ofstream target;
    ostream* op = &cout;
    if (2 < argc) {  // place output in file
        target.open(argv[2]);
        op = &target;
    }
    ostream& out = *op;
#endif

    int i;                              // for-loop variable

                                        // output command for documentation:
#if defined( STDIO )
    for (i = 0; i < argc; ++i)
        fprintf( target, "%s ", argv[i]) ;
    fprintf( target, "\n" );
#else
    for (i = 0; i < argc; ++i)
        out << argv[i] << " ";
    out << "\n";
#endif

    std::vector<T> v;                        // holds elapsed time of the tests

#if !defined( STDIO)
    #if defined (CLASSIC)
    // non-synchronized I/O is the default
    #else
    out.sync_with_stdio (false);       // must be called before any output
    #endif
#endif

    // seed the random number generator
    srand( clock() );
    clock_t t = clock();
    if (t == clock_t(-1))
    {
#if defined( STDIO )
        fprintf( stderr, "sorry, no clock\n" );
#else
        cerr << "sorry, no clock\n";
#endif
        exit(1);
    }


#if defined( STDIO )
    t = clock();
    for (i = 0; i != n; ++i)
    {
       fprintf ( target, "%d ", i);
    }
    v.push_back(T("output integers to stdio                      ", clock() - t));

    t = clock();
    for ( i = 0; i != n; ++i)
    {
       fprintf ( target, "%x ", i);
    }
    v.push_back(T("output hex integers to stdio                  ", clock() - t));

    if (clock() == clock_t(-1))
    {
        fprintf ( stderr, "sorry, clock overflow\n" );
        exit(2);
    }

    // output results
    fprintf ( stderr, "\n" );
    for (i = 0; static_cast<size_t>(i)<v.size(); i++)
        fprintf( stderr, "%s :\t%f seconds\n", v[i].s, v[i].t /CLOCKS_PER_SEC );

#else
    t = clock();
    for ( i = 0; i != n; ++i)
    {
            out << i << ' ';
    }
    v.push_back(T("output integers (sync = false)       ", clock() - t));

    out << hex;
    t = clock();
    for ( i = 0; i != n; ++i)
    {
            out << i << ' ';
    }
    v.push_back(T("output hex integers (sync = false)   ", clock() - t));

    #if defined (CLASSIC)
    out.sync_with_stdio();             // synchronize -- no argument needed
    #else
    out.sync_with_stdio (true);
    #endif

    out << dec;
    t = clock();
    for ( i = 0; i != n; ++i)
    {
            out << i << ' ';
    }
    v.push_back(T("output integers (sync = true)        ", clock() - t));

    out << hex;
    t = clock();
    for ( i = 0; i != n; ++i)
    {
            out << i << ' ';
    }
    v.push_back(T("output hex integers (sync = true)     ", clock() - t));

    if (clock() == clock_t(-1))
    {
        cerr << "sorry, clock overflow\n";
        exit(2);
    }

    // output results
    cerr << endl;
    for (i = 0; static_cast<size_t>(i) < v.size(); i++)
        cerr << v[i].s << " :\t"
            << v[i].t /CLOCKS_PER_SEC
            << " seconds" << endl;
#endif

    return 0;

}