/* Subtrai do valor anterior ao topo da pilha o valor do topo (FLOAT) */ void Mepa::SUBF() { assert(s > 0); float f1 = int2float(&m[s-1]); float f2 = int2float(&m[s]); float dif = f1 - f2; m[s-1] = float2int(&dif); s = s - 1; }
/* Multiplica o valor do topo da pilha com um valor anterior (FLOAT)*/ void Mepa::MULF() { assert( (s-1) > 0 ); float f1 = int2float(&m[s-1]); float f2 = int2float(&m[s]); float prod = f1 * f2; m[s-1] = float2int(&prod); s = s - 1; }
/* Soma o valor no topo da pilha e o da posicao anterior (FLOAT) */ void Mepa::SOMF() { assert(s > 0); float f1 = int2float(&m[s-1]); float f2 = int2float(&m[s]); float sum = f1 + f2; m[s-1] = float2int(&sum); s = s - 1; }
/* Divide o valor anterior ao topo pelo valor no topo da pilha (FLOAT) */ void Mepa::DIVF() { assert( (s-1) > 0 ); float fnum = int2float(&m[s-1]); /* numerador */ float fden = int2float(&m[s]); /* denominador */ float quoc = fnum / fden; if( fabs(fden) < 1e-10 ) { perror("Divisao por zero\n"); abort(); } m[s-1] = float2int(&quoc); s = s - 1; }
/* Comparar menor (FLOAT) */ void Mepa::CMNF() { assert(s > 0); float f1 = int2float(&m[s-1]); float f2 = int2float(&m[s]); if( f1 < f2 ) { m[s-1] = 1; } else { m[s-1] = 0; } s = s - 1; }
/* Comparar igual (FLOAT) */ void Mepa::CMIF() { assert(s > 0); float f1 = int2float(&m[s-1]); float f2 = int2float(&m[s]); if( fabs( f1 - f2 ) < 1e-10 ) { m[s-1] = 1; } else { m[s-1] = 0; } s = s - 1; }
/* Process wave file, write music probability and labels into the specified files. Return 0 on success, print error and return non-zero on error. */ int process(const char* infile, WAVE* wave, OpusSM* sm, FILE* ofp_pmusic, FILE* ofp_labels, double sm_segment_min_dur, double b_segment_min_dur ) { double frame_dur = (double)ANALYSIS_FRAME_SIZE/wave->header.SampleRate; Labeler* lb = lb_init(sm_segment_min_dur/frame_dur, b_segment_min_dur/frame_dur); float* analysis_pcm = malloc(ANALYSIS_FRAME_SIZE*wave->header.NumChannels*sizeof(float)); int16_t* buffer = malloc(ANALYSIS_FRAME_SIZE*wave->header.NumChannels*sizeof(int16_t)); double total_music_ratio = 0; int error = 0; for (int ii = 0; ii <= wave->size - ANALYSIS_FRAME_SIZE; ii = ii + ANALYSIS_FRAME_SIZE) { int readcount = wread(buffer, ANALYSIS_FRAME_SIZE, wave); error = (readcount != ANALYSIS_FRAME_SIZE); if (error) { fprintf(stderr, "Could not read from wave file \"%s\", read count %d.\n", infile, readcount); break; } int2float(buffer, analysis_pcm, ANALYSIS_FRAME_SIZE, wave->header.NumChannels); float pmusic = sm_pmusic(sm, analysis_pcm); total_music_ratio += pmusic; if (ofp_labels != NULL) { lb_add_frame(lb, pmusic); } fprintf(ofp_pmusic, "%f %f\n", (double)ii / wave->header.SampleRate, pmusic); } if (!error) { if (ofp_labels != NULL) { lb_finalize(lb); lb_print_to_file(lb, ofp_labels, frame_dur); } total_music_ratio = (total_music_ratio * (double)ANALYSIS_FRAME_SIZE) / (double) wave->size; fprintf(stderr, "Music ratio: %f\n", total_music_ratio); } lb = lb_destroy(lb); free(analysis_pcm); free(buffer); return error; }
// Filter data through filter static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) { struct mp_audio* l = af->data; // Local data struct mp_audio* c = data; // Current working data int len = c->len/c->bps; // Length in samples of current audio block if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) return NULL; // Change to cpu native endian format if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) endian(c->audio,c->audio,len,c->bps); // Conversion table if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { float2int(c->audio, l->audio, len, l->bps); if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) si2us(l->audio,len,l->bps); } else { // Input must be int // Change signed/unsigned if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ si2us(c->audio,len,c->bps); } // Convert to special formats switch(l->format&AF_FORMAT_POINT_MASK){ case(AF_FORMAT_F): int2float(c->audio, l->audio, len, c->bps); break; default: // Change the number of bits if(c->bps != l->bps) change_bps(c->audio,l->audio,len,c->bps,l->bps); else memcpy(l->audio,c->audio,len*c->bps); break; } } // Switch from cpu native endian to the correct endianness if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) endian(l->audio,l->audio,len,l->bps); // Set output data c->audio = l->audio; c->len = len*l->bps; c->bps = l->bps; c->format = l->format; return c; }
static struct mp_audio* play_s16_float(struct af_instance* af, struct mp_audio* data) { struct mp_audio* l = af->data; // Local data struct mp_audio* c = data; // Current working data int len = c->len/2; // Length in samples of current audio block if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) return NULL; int2float(c->audio, l->audio, len, 2); c->audio = l->audio; mp_audio_set_format(c, l->format); c->len = len*4; return c; }
static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data) { af_data_t* l = af->data; // Local data af_data_t* c = data; // Current working data int len = c->len/2; // Length in samples of current audio block if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) return NULL; int2float(c->audio, l->audio, len, 2); c->audio = l->audio; c->len = len*4; c->bps = 4; c->format = l->format; return c; }
static struct audio_desc audio_play_jack_query_format(void *state, struct audio_desc desc) { struct state_jack_playback *s = (struct state_jack_playback *) state; return (struct audio_desc){4, s->jack_sample_rate, min(s->jack_ports_count, desc.ch_count), AC_PCM}; } static int audio_play_jack_reconfigure(void *state, struct audio_desc desc) { struct state_jack_playback *s = (struct state_jack_playback *) state; const char **ports; int i; assert(desc.bps == 4 && desc.sample_rate == s->jack_sample_rate && desc.codec == AC_PCM); jack_deactivate(s->client); ports = jack_get_ports(s->client, s->jack_ports_pattern, NULL, JackPortIsInput); if(ports == NULL) { fprintf(stderr, "[JACK playback] Unable to input ports matching %s.\n", s->jack_ports_pattern); return FALSE; } if(desc.ch_count > s->jack_ports_count) { fprintf(stderr, "[JACK playback] Warning: received %d audio channels, JACK can process only %d.", desc.ch_count, s->jack_ports_count); } for(i = 0; i < MAX_PORTS; ++i) { ring_buffer_destroy(s->data[i]); s->data[i] = NULL; } /* for all channels previously connected */ for(i = 0; i < desc.ch_count; ++i) { jack_disconnect(s->client, jack_port_name (s->output_port[i]), ports[i]); fprintf(stderr, "[JACK playback] Port %d: %s\n", i, ports[i]); } free(s->channel); free(s->converted); s->desc.bps = desc.bps; s->desc.ch_count = desc.ch_count; s->desc.sample_rate = desc.sample_rate; s->channel = malloc(s->desc.bps * desc.sample_rate); s->converted = (float *) malloc(desc.sample_rate * sizeof(float)); for(i = 0; i < desc.ch_count; ++i) { s->data[i] = ring_buffer_init(sizeof(float) * s->jack_sample_rate); } if(jack_activate(s->client)) { fprintf(stderr, "[JACK capture] Cannot activate client.\n"); return FALSE; } for(i = 0; i < desc.ch_count; ++i) { if (jack_connect (s->client, jack_port_name (s->output_port[i]), ports[i])) { fprintf (stderr, "Cannot connect output port: %d.\n", i); return FALSE; } } free(ports); return TRUE; } static void audio_play_jack_put_frame(void *state, struct audio_frame *frame) { struct state_jack_playback *s = (struct state_jack_playback *) state; assert(frame->bps == 4); int channel_size = frame->data_len / frame->ch_count; for (int i = 0; i < frame->ch_count; ++i) { demux_channel(s->channel, frame->data, frame->bps, frame->data_len, frame->ch_count, i); int2float((char *) s->converted, (char *) s->channel, channel_size); ring_buffer_write(s->data[i], (char *) s->converted, channel_size); } } static void audio_play_jack_done(void *state) { struct state_jack_playback *s = (struct state_jack_playback *) state; int i; jack_client_close(s->client); free(s->channel); free(s->converted); free(s->jack_ports_pattern); for(i = 0; i < MAX_PORTS; ++i) { ring_buffer_destroy(s->data[i]); } free(s); } static const struct audio_playback_info aplay_jack_info = { audio_play_jack_help, audio_play_jack_init, audio_play_jack_put_frame, audio_play_jack_query_format, audio_play_jack_reconfigure, audio_play_jack_done }; REGISTER_MODULE(jack, &aplay_jack_info, LIBRARY_CLASS_AUDIO_PLAYBACK, AUDIO_PLAYBACK_ABI_VERSION);
/* Inverter sinal (FLOAT) */ inline void Mepa::INVF() { float f = int2float(&m[s]); f = -f; m[s] = float2int(&f); }
/* Imprime (FLOAT) */ void Mepa::IMPF() { float f = int2float(&m[s]); cout << f << endl; s = s - 1; }