123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- #include "sekai/MBRSynth.h"
- #include "sekai/Track.h"
- #include <string.h>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <json/json.h>
- #include <math.h>
- #include <sndfile.h>
- #include <boost/filesystem.hpp>
- #include <sekai/VoiceDefESPEAK.h>
- class VoiceDefMBR : public VoiceDef {
- private:
- float *_input_data=nullptr;
- int _input_data_length;
- int _input_data_length_ref;
- int _samplerate;
- public:
- VoiceDefMBR(std::string path,std::string ref)
- {
- SF_INFO info = {0};
- SNDFILE *sf = sf_open(path.c_str(), SFM_READ, &info);
- if (sf == nullptr) {
- return; // false
- }
- _input_data = new float[info.frames];
- _input_data_length = info.frames;
- assert(info.channels == 1);
- _samplerate = info.samplerate;
- sf_read_float(sf, _input_data, info.frames);
- sf_close(sf);
-
- if(ref.length())
- {
- SNDFILE *sf = sf_open(ref.c_str(), SFM_READ, &info);
- assert(_samplerate==info.samplerate);
- _input_data_length_ref = info.frames;
- if(sf) sf_close(sf);
- }
- }
- virtual float getLength() {
- if(_input_data_length_ref)
- return _input_data_length_ref*1.0/_samplerate;
- else
- return _input_data_length*1.0/_samplerate;
- }
- virtual void getImpulseResponse(float pos, float *impulseResponse, int *impulseResponseLength, float morph) {
- //FIXME: do not hardcode
- float frame_period = 5.0;
- int fft_size = 2048;
- float fract = pos * 1000 / frame_period;
- int frame_index = (int)fract;
- int frame_offset_l = frame_index * fft_size;
- int frame_offset_r = (frame_index+1) * fft_size;
- float pmk_interp = fract-frame_index;
- if(frame_offset_l<0 || frame_offset_r<0) return;
- for(int i=0; i<fft_size; i++)
- {
- float x=0;
- float l=0;
- float r=0;
- if(i+frame_offset_l<_input_data_length)
- l = _input_data[i+frame_offset_l];
- if(i+frame_offset_r<_input_data_length)
- r = _input_data[i+frame_offset_r];
- x = r * (1 - pmk_interp) + l * pmk_interp;
- if (morph) {
- r = x;
- l = impulseResponse[i];
- x = r * morph + l * (1.0f - morph);
- }
-
- impulseResponse[i] = x;
- }
-
- *impulseResponseLength=fft_size;
- }
- virtual int getSamplerate() {
- return _samplerate;
- }
- virtual std::string getPhoLine(int index) {
- return "";
- }
- };
- MBRSynth::MBRSynth(ControlTrack *ctrl, int buffer_size)
- : VoiceSampler(buffer_size) {
- _impulseResponse = new float[IMPULSE_RESPONSE_MAX];
- _ctrl = ctrl;
- _samplerate = 0;
- _debugfile = fopen("/tmp/maxvol.txt","w");
- }
- void MBRSynth::addUnit(const std::string &lyric, int count, float *a,float *b) {
- load(lyric);
- PhoEvent *e = new PhoEvent;
- e->points = count;
- e->voice = _voicemap[lyric];
- for (int i = 0; i < count; i++) {
- e->x[i] = a[i];
- e->y[i] = b[i];
- }
- _phoEvents.addEvent(e);
- }
- bool MBRSynth::addOnePulse() {
- float currentTime = inputPositionSamples() / _samplerate;
- if(isnan(currentTime))
- {
- printf("MBRSynth: samplerate is invalid\n");
- return false;
- }
- _phoEvents.selectNext(currentTime);
- PhoEvent *pho0 = _phoEvents.current();
- PhoEvent *pho1 = _phoEvents.next();
- if (pho0 == nullptr) return false;
- assert(_ctrl);
- float output_f0= _ctrl->getF0atTime(currentTime);
- if (output_f0 == 0) output_f0 = 50;
- if (currentTime < pho0->start()) {
- // rest: output silence
- output_f0 = 500;
- float period = _samplerate * 1.0f / output_f0;
- float dummy;
- ola(&dummy, 0, period);
- return true;
- }
- if (currentTime >= pho0->start() && currentTime < pho0->end()) {
- float interp = 0;
- int impulseResponseLength = 0;
- getImpulseResponse(currentTime, pho0, _impulseResponse,
- &impulseResponseLength,
- 0); // TODO get impulse response from mapped index
- if (pho1 && currentTime >= pho1->start()) {
- interp = (currentTime - pho1->start()) / (pho0->end() - pho1->start());
- }
- if (interp > 0) {
- getImpulseResponse(currentTime, pho1, _impulseResponse,
- &impulseResponseLength,
- interp); // needs interp as input
- }
- // int tmp = static_cast<int>(_samplerate * 1.0f / output_f0);
- // float period = tmp;
- float period = _samplerate * 1.0f / output_f0;
- VoiceSampler::hanningWindow(_impulseResponse, impulseResponseLength);
- ola(_impulseResponse, impulseResponseLength, period);
- return true;
- }
- {
- // rest: output silence
- output_f0 = 500;
- float period = _samplerate * 1.0f / output_f0;
- float dummy;
- ola(&dummy, 0, period);
- return true;
- }
- return false;
- }
- void MBRSynth::postProcess(float* data, int size)
- {
- float thresh=0.95;
- for(int i=0;i<size;i++)
- {
- float t = (i + outputPos)*1.0/_samplerate;
- data[i] *= _ctrl->getDynamicsAtTime(t);
- float vol = fabs(data[i]);
- if(vol > _maxvol) _maxvol = vol;
- if(_maxvol > thresh)
- {
- float a = thresh/_maxvol;
- data[i] *= a;
- }
- }
- fprintf(_debugfile,"maxvol = %f\n",_maxvol);
-
- }
- void MBRSynth::getImpulseResponse(float currentTime, PhoEvent *event,
- float *impulseResponse,
- int *impulseResponseLength, float morph) {
- auto voice = event->voice;
- float localTime =
- interp_linear(event->x, event->y, event->points, currentTime);
- assert(voice);
- if(voice==nullptr) return;
- voice->getImpulseResponse(localTime,impulseResponse,impulseResponseLength,morph);
- //TODO: morph is ignored
- }
- float MBRSynth::getLengthForUnit(const std::string &fileName) {
- load(fileName);
- return _voicemap[fileName]->getLength();
- }
- std::string MBRSynth::getPhoLine(const std::string &fileName, int index)
- {
- load(fileName);
- return _voicemap[fileName]->getPhoLine(index);
- }
- void MBRSynth::load(const std::string &unit)
- {
- if (_voicemap[unit] == nullptr) {
-
- synthType type = synthType::FVOX;
- if(_config) type = _config->type;
- switch(type)
- {
- case synthType::UTAU:
- {
- std::string debug = _basedir+"/"+unit+".ogg";
- _voicemap[unit] = new VoiceDefMBR(_basedir+"/"+unit+".ogg",_basedir+"/"+unit+".wav");
- break;
- }
- case synthType::ESPEAK:
- {
- _voicemap[unit] = new VoiceDefESPEAK(unit);
- break;
- }
- case synthType::FVOX:
- {
- printf("VoiceDefFOX %s %s\n",_basedir.c_str(),unit.c_str());
- _voicemap[unit] = new VoiceDefMBR(_basedir+"/ogg/"+unit+".ogg",_basedir+"/wav/"+unit+".wav");
- break;
- }
- default:
- {
- printf("MBRSynth::load failed %s\n",unit.c_str());
- throw new std::runtime_error("unknown voice type");
- }
- }
-
- if(_samplerate==0) {
- _samplerate=_voicemap[unit]->getSamplerate();
- }
- else
- assert(_samplerate==_voicemap[unit]->getSamplerate());
- }
- }
- bool MBRSynth::saveWave(const std::string &filename)
- {
- SF_INFO info = {0};
- info.samplerate = _samplerate;
- info.channels = 1;
- info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
- SNDFILE *sf = sf_open(filename.c_str(), SFM_WRITE, &info);
- if(sf==nullptr) return false;
- while (1) {
- const int size = 1024;
- int fill = size * 4;
- float buffer_out[size];
- if (readData(buffer_out, size, fill) == false) break;
- sf_write_float(sf, buffer_out, size);
- }
- sf_close(sf);
- return true;
- }
|