int main(int argc, char **argv) { int i, r; struct format *f; const char *input = NULL, *output = NULL; FILE *out; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "--format")) { if (i + 1 == argc) usage(); f = lookup_format(argv[i + 1]); if (!f) { fprintf(stderr, "unkown format\n"); usage(); } i++; } else if (!strcmp(argv[i], "--debug")) { yydebug = 1; } else if (!strcmp(argv[i], "-o")) { if (i + 1 == argc) usage(); output = argv[i + 1]; i++; } else { if (input) { fprintf(stderr, "multiple input files specified.\n"); usage(); } input = argv[i]; } } if (!input) { fprintf(stderr, "no input file specified\n"); usage(); } if (!f) { fprintf(stderr, "no format specified\n"); usage(); } init(); yyin = fopen(input, "r"); if (!yyin) { fprintf(stderr, "Couldn't open '%s'\n", input); exit(1); } if (!output) set_output_file(stdout); else { out = fopen(output, "w"); if (!out) { fprintf(stderr, "unable to open '%s'", output); exit(1); } set_output_file(out); } r = yyparse(); f->fn(get_result()); fclose(yyin); if (output) fclose(out); fin(); return r; }
/** * Check the commandline arguments * @param argc number of commandline args+1 * @param argv array of arguments, first is program name * @param s the stripper object containing local vars * @return 1 if they were OK, 0 otherwise */ static int check_args( int argc, char **argv, stripper *s ) { char *dot_pos; int sane = 1; if ( argc < 2 ) sane = 0; else { int i,rlen; const char *rdata = NULL; for ( i=1;i<argc;i++ ) { if ( strlen(argv[i])==2 && argv[i][0]=='-' ) { switch ( argv[i][1] ) { case 'v': printf( "stripper version 1.1 (c) " "Desmond Schmidt 2011\n"); s->doing_help = 1; break; case 'h': print_help(); s->doing_help = 1; break; case 'f': if ( i < argc-2 ) { s->selected_format = lookup_format( argv[i+1] ); if ( s->selected_format == -1 ) error("stripper: format %s not supported.\n", argv[i+1]); } else sane = 0; break; case 'l': list_formats(); s->doing_help = 1; break; case 'r': s->recipe_file = argv[i+1]; break; case 's': s->style = argv[i+1]; break; case 'e': s->hh_except_string = strdup(argv[i+1]); break; } } if ( !sane ) break; } if ( !s->doing_help ) { strncpy( s->src, argv[argc-1], FILE_NAME_LEN ); sane = file_exists( s->src ); if ( !sane ) fprintf(stderr,"stripper: can't find file %s\n",s->src ); else { strncpy(s->barefile, s->src, FILE_NAME_LEN ); dot_pos = strrchr( s->barefile, '.' ); if ( dot_pos != NULL ) dot_pos[0] = 0; } } } return sane; }
double sound_overwrite( LVAL snd_expr, long n, unsigned char *filename, double offset_secs, long format, long mode, long bits, long swap, double *duration) { LVAL result; // the SOUND to be evaluated SF_INFO sf_info; // info about the sound file double max_sample; // return value long ntotal; // how many samples were overwritten /* long flags; */ // first check if sound file exists, do not create new file FILE *file = NULL; if (ok_to_open((char *) filename, "rb")) file = fopen((char *) filename, "rb"); // if not then fail if (!file) { *duration = 0; return 0.0; } else { fclose(file); } memset(&sf_info, 0, sizeof(sf_info)); sf_info.format = lookup_format(format, mode, bits, swap); result = xleval(snd_expr); /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */ if (vectorp(result)) { SNDFILE *sndfile; // opened sound file float *buf; // buffer for samples read in from sound file /* make sure all elements are of type a_sound */ long i = getsize(result); long channels = i; while (i > 0) { i--; if (!exttypep(getelement(result, i), a_sound)) { xlerror("sound_save: array has non-sound element", result); } } sndfile = open_for_write(filename, SFM_RDWR, format, &sf_info, channels, ROUND(getsound(getelement(result, 0))->sr), offset_secs, &buf); max_sample = sound_save_array(result, n, &sf_info, sndfile, buf, &ntotal, NULL); *duration = ntotal / (double) sf_info.samplerate; free(buf); sf_close(sndfile); } else if (exttypep(result, a_sound)) { SNDFILE *sndfile; // opened sound file float *buf; // buffer for samples read in from sound file sndfile = open_for_write(filename, SFM_RDWR, format, &sf_info, 1, ROUND(getsound(result)->sr), offset_secs, &buf); max_sample = sound_save_sound(result, n, &sf_info, sndfile, buf, &ntotal, NULL); *duration = ntotal / (double) sf_info.samplerate; free(buf); sf_close(sndfile); } else { xlerror("sound_save: expression did not return a sound", result); max_sample = 0.0; } return max_sample; }
/* * Class: calliope_AeseStripper * Method: strip * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLcalliope/json/JSONResponse;Lcalliope/json/JSONResponse;)I */ JNIEXPORT jint JNICALL Java_calliope_AeseStripper_strip (JNIEnv *env, jobject obj, jstring xml, jstring rules, jstring format, jstring style, jstring language, jstring hexcepts, jboolean html, jobject text, jobject markup) { int res = 1; jboolean x_copied,r_copied=JNI_FALSE,f_copied,s_copied,h_copied,l_copied=JNI_FALSE; const char *x_str = load_string( env, xml, &x_copied ); //fprintf(stderr,"x_str=%s r_str\n",x_str); const char *r_str = (rules!=NULL)?load_string(env,rules,&r_copied):NULL; //fprintf(stderr,"r_str=%s\n",r_str); const char *f_str = load_string( env, format, &f_copied ); //fprintf(stderr,"f_str=%s\n",f_str); const char *s_str = load_string( env, style, &s_copied ); //fprintf(stderr,"s_str=%s\n",s_str); const char *l_str = (language==NULL)?"en_GB" :load_string( env, language, &l_copied ); //fprintf(stderr,"l_str=%s\n",l_str); const char *h_str = (hexcepts==NULL)?NULL :load_string( env, hexcepts, &h_copied ); //fprintf(stderr,"h_str=%s\n",h_str); stripper *s = stripper_create(); if ( s != NULL ) { recipe *ruleset; s->hh_except_string = (h_str==NULL)?NULL:strdup(h_str); s->selected_format = lookup_format( f_str ); // load or initialise rule set if ( rules == NULL ) ruleset = recipe_new(); else ruleset = recipe_load(r_str,strlen(r_str)); hh_exceptions *hhe = hh_exceptions_create( s->hh_except_string ); if ( hhe != NULL ) { s->user_data = userdata_create( s->language, s->barefile, ruleset, &formats[s->selected_format], hhe ); if ( s->user_data != NULL ) { // write header int i=0; while ( res && userdata_markup_dest(s->user_data,i)!= NULL ) { res = formats[s->selected_format].hfunc( NULL, dest_file_dst( userdata_markup_dest(s->user_data,i)), s_str ); i++; } // parse XML if ( res ) { int xlen = strlen( x_str ); res = scan_source( x_str, xlen, s ); if ( res ) userdata_write_files( env, s->user_data, text, markup ); } else tmplog("write header failed\n"); } } stripper_dispose( s ); unload_string( env, xml, x_str, x_copied ); unload_string( env, rules, r_str, r_copied ); unload_string( env, format, f_str, f_copied ); unload_string( env, style, s_str, s_copied ); unload_string( env, language, l_str, l_copied ); if ( h_str != NULL ) unload_string( env, hexcepts, h_str, h_copied ); } return res; }
double sound_save( LVAL snd_expr, long n, unsigned char *filename, long format, long mode, long bits, long swap, double *sr, long *nchans, double *duration, LVAL play) { LVAL result; float *buf; long ntotal; double max_sample; SNDFILE *sndfile = NULL; SF_INFO sf_info; PaStream *audio_stream = NULL; if (SAFE_NYQUIST) play = FALSE; gc(); memset(&sf_info, 0, sizeof(sf_info)); sf_info.format = lookup_format(format, mode, bits, swap); result = xleval(snd_expr); /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */ if (vectorp(result)) { /* make sure all elements are of type a_sound */ long i = getsize(result); *nchans = sf_info.channels = i; while (i > 0) { i--; if (!exttypep(getelement(result, i), a_sound)) { xlerror("sound_save: array has non-sound element", result); } } /* assume all are the same: */ *sr = sf_info.samplerate = ROUND(getsound(getelement(result, 0))->sr); /* note: if filename is "", then don't write file; therefore, * write the file if (filename[0]) */ if (filename[0]) { sndfile = NULL; if (ok_to_open((char *) filename, "wb")) sndfile = sf_open((char *) filename, SFM_WRITE, &sf_info); if (sndfile) { /* use proper scale factor: 8000 vs 7FFF */ sf_command(sndfile, SFC_SET_CLIPPING, NULL, SF_TRUE); } } if (play) play = prepare_audio(play, &sf_info, &audio_stream); if ((buf = (float *) malloc(max_sample_block_len * sf_info.channels * sizeof(float))) == NULL) { xlabort("snd_save -- couldn't allocate memory"); } max_sample = sound_save_array(result, n, &sf_info, sndfile, buf, &ntotal, audio_stream); *duration = ntotal / *sr; if (sndfile) sf_close(sndfile); if (play != NIL) finish_audio(audio_stream); } else if (exttypep(result, a_sound)) { *nchans = sf_info.channels = 1; sf_info.samplerate = ROUND((getsound(result))->sr); *sr = sf_info.samplerate; if (filename[0]) { sndfile = NULL; if (ok_to_open((char *) filename, "wb")) { sndfile = sf_open((char *) filename, SFM_WRITE, &sf_info); if (sndfile) { /* use proper scale factor: 8000 vs 7FFF */ sf_command(sndfile, SFC_SET_CLIPPING, NULL, SF_TRUE); } else { char error[240]; sprintf(error, "snd_save -- %s", sf_error_number(sf_error(sndfile))); xlabort(error); } } else { xlabort("snd_save -- write not permitted by -W option"); } } if (play) play = prepare_audio(play, &sf_info, &audio_stream); if ((buf = (float *) malloc(max_sample_block_len * sizeof(float))) == NULL) { xlabort("snd_save -- couldn't allocate memory"); } max_sample = sound_save_sound(result, n, &sf_info, sndfile, buf, &ntotal, audio_stream); *duration = ntotal / *sr; if (sndfile) sf_close(sndfile); if (play != NIL) finish_audio(audio_stream); } else { xlerror("sound_save: expression did not return a sound", result); max_sample = 0.0; } free(buf); return max_sample; }