void Vocoder::reconfigure() { need_reconfigure = 0; if(current_bands != config.bands) { current_bands = config.bands; for(int i = 0; i < config.bands; i++) { formant_bands[i].reset(); double a = 16.0 * i / (double)current_bands; if(a < 4.0) formant_bands[i].freq = 150 + 420 * a / 4.0; else formant_bands[i].freq = 600 * pow (1.23, a - 4.0); double c = formant_bands[i].freq * 2 * M_PI / get_samplerate(); formant_bands[i].c = c * c; formant_bands[i].f = 0.4 / c; formant_bands[i].att = 1 / (6.0 + ((exp (formant_bands[i].freq / get_samplerate()) - 1) * 10)); carrier_bands[i].copy_from(&formant_bands[i]); output_bands[i].decay = decay_table[(int)a]; } } }
int Caud_file::extract_as_wav(const string& name) { Cvirtual_binary d = decode(); if (!d.data()) return 1; Cfile32 f; int error = f.open(name, GENERIC_WRITE); if (error) return error; int cb_sample = get_cb_sample(); int cs_remaining = get_c_samples(); t_wav_header header; memset(&header, 0, sizeof(t_wav_header)); header.file_header.id = wav_file_id; header.file_header.size = sizeof(header) - sizeof(header.file_header) + (cs_remaining << 1); header.form_type = wav_form_id; header.format_chunk.header.id = wav_format_id; header.format_chunk.header.size = sizeof(header.format_chunk) - sizeof(header.format_chunk.header); header.format_chunk.formattag = 1; header.format_chunk.c_channels = 1; header.format_chunk.samplerate = get_samplerate(); header.format_chunk.byterate = cb_sample * get_samplerate(); header.format_chunk.blockalign = cb_sample; header.format_chunk.cbits_sample = cb_sample << 3; header.data_chunk_header.id = wav_data_id; header.data_chunk_header.size = cb_sample * cs_remaining; error = f.write(&header, sizeof(t_wav_header)); return error ? error : f.write(d); }
UINT CSoundFile::Read( LPVOID lpBuffer, UINT cbBuffer ) { mpcpplog(); if ( !mod ) { return 0; } mpcpplog(); if ( !lpBuffer ) { return 0; } mpcpplog(); if ( cbBuffer <= 0 ) { return 0; } mpcpplog(); if ( get_samplerate() <= 0 ) { return 0; } mpcpplog(); if ( get_sample_size() != 1 && get_sample_size() != 2 && get_sample_size() != 4 ) { return 0; } mpcpplog(); if ( get_num_channels() != 1 && get_num_channels() != 2 && get_num_channels() != 4 ) { return 0; } mpcpplog(); std::memset( lpBuffer, 0, cbBuffer ); const std::size_t frames_torender = cbBuffer / get_frame_size(); std::int16_t * out = (std::int16_t*)lpBuffer; std::vector<std::int16_t> tmpbuf; if ( get_sample_size() == 1 || get_sample_size() == 4 ) { tmpbuf.resize( frames_torender * get_num_channels() ); out = &tmpbuf[0]; } mod->set_render_param( openmpt::module::RENDER_INTERPOLATIONFILTER_LENGTH, get_filter_length() ); std::size_t frames_rendered = 0; if ( get_num_channels() == 1 ) { frames_rendered = mod->read( get_samplerate(), frames_torender, out ); } else if ( get_num_channels() == 4 ) { frames_rendered = mod->read_interleaved_quad( get_samplerate(), frames_torender, out ); } else { frames_rendered = mod->read_interleaved_stereo( get_samplerate(), frames_torender, out ); } if ( get_sample_size() == 1 ) { std::uint8_t * dst = (std::uint8_t*)lpBuffer; for ( std::size_t sample = 0; sample < frames_rendered * get_num_channels(); ++sample ) { dst[sample] = ( tmpbuf[sample] / 0x100 ) + 0x80; } } else if ( get_sample_size() == 4 ) { std::int32_t * dst = (std::int32_t*)lpBuffer; for ( std::size_t sample = 0; sample < frames_rendered * get_num_channels(); ++sample ) { dst[sample] = tmpbuf[sample] << (32-16-1-MIXING_ATTENUATION); } } return frames_rendered * get_frame_size(); }
void nova_server::prepare_backend(void) { /* register audio backend ports */ const int blocksize = get_audio_blocksize(); const int input_channels = get_input_count(); const int output_channels = get_output_count(); std::vector<sample*> inputs, outputs; for (int channel = 0; channel != input_channels; ++channel) inputs.push_back(sc_factory->world.mAudioBus + (blocksize * (output_channels + channel))); audio_backend::input_mapping(inputs.begin(), inputs.end()); for (int channel = 0; channel != output_channels; ++channel) outputs.push_back(sc_factory->world.mAudioBus + blocksize * channel); audio_backend::output_mapping(outputs.begin(), outputs.end()); #ifdef __SSE__ /* denormal handling */ _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _mm_setcsr(_mm_getcsr() | 0x40); #endif time_per_tick = time_tag::from_samples(blocksize, get_samplerate()); }
wav_file *open_wav(char *filename) { wav_file *state = (wav_file*)malloc(sizeof(wav_file)); state->file = fopen(filename,"rb"); if(state->file==NULL) { free(state); return(NULL); } state->filename = (char*)malloc(strlen(filename)+1); strcpy(state->filename,filename); state->header = (wav_header*)malloc(sizeof(wav_header)); state->sample_buffer=NULL; state->sample_buffer_length=0; memset(state->header,0,sizeof(wav_header)); int r = fread((void*)state->header,sizeof(wav_header),1,state->file); if(!r) { close_wav(state); return(NULL); } /*header sanity check*/ if(memcmp(state->header->riff_magic,&RIFF_MAGIC,4) || memcmp(state->header->wave_magic,&WAVE_MAGIC,4) || memcmp(state->header->wave_chunk_magic,&WAVE_CHUNK_MAGIC,4) || memcmp(state->header->wave_chunk_data_magic,&WAVE_CHUNK_DATA_MAGIC,4) || state->header->format_tag != PCM_FORMAT_TAG) { printf("sanity check failed\n"); close_wav(state); return(NULL); } state->offset = WAVE_HEADER_LENGTH; //set offset to beginning of data printf("samplerate: %dhz\n",get_samplerate(state)); printf("bitrate: %d\n",state->header->bitrate); printf("channels: %d\n",state->header->channels); printf("bits per sample: %d\n",state->header->bits_per_sample); printf("block align: %d\n",state->header->block_align); printf("format_tag: %d\n",state->header->format_tag); state->samples_num = state->header->wave_chunk_data_length/state->header->block_align; //use a 1000 samples buffer unsigned long sample_buffer_num = 1000; if(state->samples_num<sample_buffer_num) sample_buffer_num = state->samples_num; state->sample_buffer_length = state->header->block_align*sample_buffer_num; state->sample_buffer = (void*)malloc(state->sample_buffer_length); printf("created a %d bytes samples buffer\n",state->sample_buffer_length); printf("number of samples: %d\n",state->samples_num); if(!fill_sample_buffer(state)) { close_wav(state); return(NULL); } return(state); }
bool ModuleSequencer::update_module_lists() { if (!get_buffersize() || !get_samplerate()) { return false; } if (prepare_module_lists()) { commit_module_lists(); if (stateflags & SF_OVERLOAD) { // hack: jackd need some time for new load statistic Glib::signal_timeout().connect_once( sigc::bind( sigc::mem_fun(this,&ModuleSequencer::clear_stateflag), SF_OVERLOAD), 1000); } return true; } return false; }
double Synth::solve_eqn(double *output, int length, double freq, double normalize_constant, int oscillator) { SynthOscillatorConfig *config = this->config.oscillator_config.values[oscillator]; if(config->level <= INFINITYGAIN) return 0; double power = this->db.fromdb(config->level) * normalize_constant; // Period of fundamental frequency in samples double orig_period = (double)get_samplerate() / freq; // Starting sample in waveform double x = waveform_sample; double phase_offset = config->phase * orig_period; //printf("Synth::solve_eqn %d %f\n", __LINE__, config->phase); // Period of current oscillator double period = orig_period / config->freq_factor; int sample; double step = 1; if(get_direction() == PLAY_REVERSE) step = -1; switch(this->config.wavefunction) { case DC: for(sample = 0; sample < length; sample++) { output[sample] += power; } break; case SINE: for(sample = 0; sample < length; sample++) { output[sample] += sin((x + phase_offset) / period * 2 * M_PI) * power; x += step; } break; case SAWTOOTH: for(sample = 0; sample < length; sample++) { output[sample] += function_sawtooth((x + phase_offset) / period) * power; x += step; } break; case SQUARE: for(sample = 0; sample < length; sample++) { output[sample] += function_square((x + phase_offset) / period) * power; x += step; } break; case TRIANGLE: for(sample = 0; sample < length; sample++) { output[sample] += function_triangle((x + phase_offset) / period) * power; x += step; } break; case PULSE: for(sample = 0; sample < length; sample++) { output[sample] += function_pulse((x + phase_offset) / period) * power; x += step; } break; case NOISE: for(sample = 0; sample < length; sample++) { output[sample] += function_noise() * power; } break; } return 0; }
int Vocoder::process_buffer(int64_t size, Samples **buffer, int64_t start_position, int sample_rate) { need_reconfigure |= load_configuration(); if(need_reconfigure) reconfigure(); // Process all except output channel int carrier_track = config.carrier_track; CLAMP(carrier_track, 0, PluginClient::get_total_buffers() - 1); int formant_track = 0; if(carrier_track == 0) formant_track = 1; CLAMP(formant_track, 0, PluginClient::get_total_buffers() - 1); // Copy level controls to band levels for(int i = 0; i < current_bands; i++) { output_bands[i].level = 1.0; } for(int i = 0; i < get_total_buffers(); i++) { read_samples(buffer[i], i, get_samplerate(), start_position, size); } double *carrier_samples = buffer[carrier_track]->get_data(); double *formant_samples = buffer[formant_track]->get_data(); double *output = buffer[0]->get_data(); double wetness = DB::fromdb(config.wetness); double level = DB::fromdb(config.level); for(int i = 0; i < size; i++) { do_bandpasses(carrier_bands, carrier_samples[i]); do_bandpasses(formant_bands, formant_samples[i]); output[i] *= wetness; double accum = 0; for(int j = 0; j < current_bands; j++) { output_bands[j].oldval = output_bands[j].oldval + (fabs (formant_bands[j].y) - output_bands[j].oldval) * output_bands[j].decay; double x = carrier_bands[j].y * output_bands[j].oldval; accum += x * output_bands[j].level; } accum *= level; output[i] += accum; } return 0; }
int sal_audio_init(sal_audio_s *audio) { CHECK(NULL == g_audio_args, HI_FAILURE, "reinit error, please exit first.\n"); CHECK(NULL != audio, HI_FAILURE, "null ptr error.\n"); CHECK(audio->enable, HI_FAILURE, "audio enable %#x.\n", audio->enable); HI_S32 s32Ret = -1; g_audio_args = (sal_audio_args_s*)malloc(sizeof(sal_audio_args_s)); CHECK(g_audio_args != NULL, HI_FAILURE, "malloc %d bytes failed.\n", sizeof(sal_audio_args_s)); memset(g_audio_args, 0, sizeof(sal_audio_args_s)); pthread_mutex_init(&g_audio_args->mutex, NULL); memcpy(&g_audio_args->audio, audio, sizeof(*audio)); audio_vqe_config_load(); g_audio_args->input_vol = 26; g_audio_args->mic_vol = 4; g_audio_args->output_vol = 0; // [-121,6] g_audio_args->dec_buf = (unsigned char *)malloc(g_audio_args->audio.ptNumPerFrm*4); CHECK(g_audio_args->dec_buf != NULL, -1, "malloc %d bytes failed.\n", sizeof(sal_audio_args_s)); AIO_ATTR_S stAioAttr; memset(&stAioAttr, 0, sizeof(stAioAttr)); stAioAttr.enSamplerate = get_samplerate(); stAioAttr.enBitwidth = get_bitwidth(); stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = get_sound_mode(); stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = g_audio_args->audio.ptNumPerFrm; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 0; s32Ret = acodec_init(stAioAttr.enSamplerate); CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret); s32Ret = audio_aenc_start(&stAioAttr); CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret); s32Ret = audio_adec_start(&stAioAttr); CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret); if (g_audio_args->vqe_cfg.vqe_enbale) { s32Ret = audio_vqe_start(); CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret); } acodec_set_input_vol(g_audio_args->input_vol); acodec_set_mic_gain(g_audio_args->mic_vol); acodec_set_output_vol(g_audio_args->output_vol); sal_audio_capture_volume_set(g_audio_args->audio.volume); g_audio_args->last_aw8733a_stat = 0; s32Ret = audio_aw8733a(g_audio_args->last_aw8733a_stat); CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret); util_time_abs(&g_audio_args->last_aw8733a_time); return HI_SUCCESS; }