コード例 #1
0
int main(int argc, char** argv){
	if(argc != 4){
		printf("Usage: %s [num_samples] [file1] [file2]\n", argv[0]);
		return 0;
	}

	int n = atoi(argv[1]);
	FILE* fin1 = fopen(argv[2],"r");
	FILE* fin2 = fopen(argv[3],"r");

	complex_t error[n];	

	int i = 0;
	complex_t temp1;
	complex_t temp2;
	for( i = 0;i < n; ++i){

		// these might need to be %f instead of %lf
		fscanf(fin1,"%lf %lf",&temp1.re, &temp1.im);
		fscanf(fin2,"%lf %lf",&temp2.re, &temp2.im);
	//	printf("%f %f, %f %f\n", temp1.re, temp1.im, temp2.re, temp2.im);

		// get the difference between the two complex numbers
		// real and imaginary parts
		error[i].re = fabs(temp1.re - temp2.re);
		error[i].im = fabs(temp1.im - temp2.im);
	}
	fclose(fin1);
	fclose(fin2);


	//print_complex_array(error, n);

	// root mean square
	root_mean_square(error,n);

	return 0;
}
コード例 #2
0
ファイル: lurker.c プロジェクト: wader/lurker
int lurk()
{
    wav_file in, out;
    int quit;
    int16_t *buffer;
    int buffer_length, buffer_bytes, read_length;
    uint64_t total_length, cut_length, peak_length;
    int recording;
    char *output_path, *output_temp_path;
    double rms;
    const char progress[] = {'|', '/', '-', '\\'};
    uint64_t progress_temp, progress_position;

    quit = 0;
    terminate_signal = 0;
    output_path = NULL;
    output_temp_path = NULL;
    progress_position = 0;
    
    printf("Reading header from %s\n", (input == NULL ? "stdin" : input));

    if(wav_open_read(input, &in) == -1)
    {
        fprintf(stderr, "Failed to open %s for input\n", input);

        return -1;
    }
    
    if(!(in.format.audio_format == 1 && /* 1 = PCM */
         in.format.bits_per_sample == 16 &&
         in.format.num_channels == 1))
    {
        fprintf(stderr, "Wrong audio format, i want 16 bit mono PCM audio\n");

        return -1;
    }
    
    printf("Output: %s\n", output);
    printf("Recording append: %s\n", recording_append);
    printf("Threshold: %g\n", threshold);
    printf("Runlength: %g seconds\n", runlength);
    if(short_filter != 0)
        printf("Short filter: %g seconds\n", short_filter);
   
    buffer_length = in.format.sample_rate / slice_divisor;
    buffer_bytes = buffer_length * in.format.block_align;
    buffer = malloc(buffer_bytes);
    if(buffer == NULL)
    {
        fprintf(stderr, "lurk: malloc audio buffer failed\n");

        return -1;
    }
    total_length = 0;
    cut_length = 0;
    peak_length = 0;
    recording = 0;

    if(time_start != 0)
    {
        char s[256];
        
        strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&time_start));
        printf("Start time: %s\n", s);
    }
    printf("Sample rate: %d Hz\n", in.format.sample_rate);
    printf("Slice divisor: %g\n", slice_divisor);
    printf("\n");
    printf("Starting to lurk...\n");
    
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    
    while(quit == 0)
    {
        read_length = riff_read_wave_16(in.stream, buffer, buffer_length);
        if(read_length < 1)
        {
            if(read_length == 0)
            {
                if(recording == 1)
                    quit = 1; /* run loop one last time */
                else
                    break;
            }
            else
            {
                fprintf(stderr, "Error reading input file\n");

                if(recording == 1)
                {
                    message("Trying to close output file nicely\n");
                    wav_close_write(&out);
                    rename(output_temp_path, output_path);
                }

                break;
            }
        }

        if(terminate_signal == 1)
            quit = 1;
        
        rms = root_mean_square(buffer, read_length);
        total_length += read_length;
        
        if(recording == 1)
        {
            if((double)peak_length / in.format.sample_rate > runlength || quit == 1)
            {
                /* stop recording to file */
                recording = 0;

                /* remove runlength seconds of silence at end of recording */
                cut_length -= runlength * in.format.sample_rate;
                /* adjust file */
                wav_truncate(&out, cut_length * out.format.block_align);
                
                if(wav_close_write(&out) == -1)
                    fprintf(stderr, "lurk: failed to close output file %s\n", output_temp_path);
                
                if(short_filter != 0 &&
                   (double)cut_length / in.format.sample_rate < short_filter)
                {
                    if(unlink(output_temp_path) == -1)
                        fprintf(stderr, "lurk: faild to unlink %s\n", output_temp_path);
                    
                    message("Recording removed, short filter\n");
                }
                else
                {
                    if(rename(output_temp_path, output_path) == -1)
                        fprintf(stderr, "lurk: faild to rename %s to %s\n", output_temp_path, output_path);
                    
                    message("Recording stopped, %d minutes %d seconds recorded\n",
                            (int)(cut_length / in.format.sample_rate) / 60,
                            /* (int)(ceil(cut_length / in.format.sample_rate)) % 60 */
                            (int)(cut_length / in.format.sample_rate) % 60
                            );
                }
                
                free(output_path);
                free(output_temp_path);
                output_path = NULL;
                output_temp_path = NULL;
                cut_length = 0;
                peak_length = 0;
            }
            else
            {
                cut_length += read_length;
                peak_length += read_length;

                if(rms > threshold)
                    peak_length = 0;
            }
        }
        else
        {
            if(rms > threshold)
            {
                time_t t;
                char *s, *d;
                char expanded[PATH_MAX];
                
                /* start recording to file */
                recording = 1;

                /* fancy print output path (non-absolute path etc) */
                if(time_start != 0)
                    t = time_start + total_length / in.format.sample_rate;
                else
                    time(&t);
                strftime(expanded, sizeof(expanded), output, localtime(&t));
                message("Recording started to %s\n", expanded);

                if(asprintf(&output_path, "%s%s",
                            (output[0] == '/' ? "" : current_dir), /* make absolute if relative */
                            expanded
                            ) == -1)
                {
                    fprintf(stderr, "lurk: asprintf failed: output_path\n");

                    return -1;
                }
                if(asprintf(&output_temp_path, "%s%s",
                            output_path,
                            recording_append
                            ) == -1)
                {
                    fprintf(stderr, "lurk: asprintf failed: output_temp_path\n");

                    return -1;
                }
                
                s = strdup(output_path);
                if(s == NULL)
                {
                    fprintf(stderr, "lurk: strdup failed: output_path\n");

                    return -1;
                }
                d = dirname(s);
                if(mkdirp(d) == -1)
                {
                    fprintf(stderr, "lurk: mkdirp failed: %s\n", d);

                    return -1;
                }
                free(s);
                
                out.riff.size = INT32_MAX; /* as big as possible, wav_close_write will fix them */
                out.data.size = INT32_MAX;
                out.format.audio_format = 1; /* PCM */
                out.format.num_channels = 1; /* mono */
                out.format.sample_rate = in.format.sample_rate;
                out.format.byte_rate = in.format.byte_rate;
                out.format.block_align = in.format.block_align;
                out.format.bits_per_sample = in.format.bits_per_sample;

                if(wav_open_write(output_temp_path, &out) == -1)
                {
                    fprintf(stderr, "lurk: failed to open temp output file %s\n", output_temp_path);

                    return -1;
                }
            }
        }
/*
        progress_temp = total_length / in.format.sample_rate;
        if(progress_temp > progress_position)
        {
            progress_position = progress_temp;
            message("%s %c",
                   (recording == 1 ? "Recording" : "Lurking"),
                   progress[progress_position % sizeof(progress)]
                   );

            fflush(stdout);
        }
*/        

        /* bloated fancy status featuring cut length, volume-meter and more! */
        {
            int p, l;
            char b[21];
            
            progress_position = total_length / in.format.sample_rate;
            
            l = sizeof(b) * rms;
            for(p = 0; p < sizeof(b) - 1; p++)
                b[p] = (p < l ? '=' : ' ');
            b[sizeof(b) - 1] = '\0';
            
            message("%s %c [t:%.1f c:%.1f p:%.1f] [%s]",
                   (recording == 1 ? "Recording" : "Lurking"),
                   progress[progress_position % sizeof(progress)],
                   (double)total_length / in.format.sample_rate,
                   (double)cut_length / in.format.sample_rate,
                   (double)peak_length / in.format.sample_rate,
                   b
                   );
            
            fflush(stdout);
        }
        
        if(recording == 1)
        {
            /* write audio to file */
            if(riff_write_wave_16(out.stream, buffer, read_length) == -1)
            {
                fprintf(stderr, "lurk: riff_write_wave_16 failed\n");

                return -1;
            }
        }
    }

    wav_close_read(&in);
    free(buffer);
    if(output_path != NULL)
        free(output_path);
    if(output_temp_path != NULL)
        free(output_temp_path);
    
    message("Stopped\n");

    return 0;
}