public override void enforceSampleRate(int sampleRate)
 {
     //The SFZ instrument automatically enforces the SampleRate in
     //ParseInstrumentData(...) so for the most part this will not be needed...
     if (sampleRate == this.SampleRate)
     {
         return;
     }
     //Enforce SampleRate on Parameters
     for (int x = 0; x < regions.Length; x++)
     {
         //Sample s = base.SampleList[regions[x].SampleIndex];
         float factor = (sampleRate / this.SampleRate);
         regions[x].LoopEnd   = (int)(regions[x].LoopEnd * factor);
         regions[x].LoopStart = (int)(regions[x].LoopStart * factor);
         regions[x].Offset    = (int)(regions[x].Offset * factor);
         regions[x].Attack    = (int)(regions[x].Attack * factor);
         regions[x].Release   = (int)(regions[x].Release * factor);
         regions[x].Decay     = (int)(regions[x].Decay * factor);
         regions[x].Hold      = (int)(regions[x].Hold * factor);
     }
     //Enforce SampleRate on Samples
     for (int x = 0; x < base.SampleList.Length; x++)
     {
         Sample s      = base.SampleList[x];
         float  factor = sampleRate / (float)s.SampleRate;
         if (factor != 1.0f)
         {
             s.setAllSampleData(WaveHelper.ReSample(sampleRate, s.SampleRate, s.getAllSampleData()));
             s.SampleRate = sampleRate;
         }
     }
     this.SampleRate = sampleRate;
 }
        public ModelToWaveService(ThroatModelParameters parameters)
        {
            if (parameters.TubeLength <= 0)
            {
                throw new Exception("Illegal tube length.");
            }

            _parameters = parameters;
            ///CALCULATE THE SAMPLE RATE, BASED ON NOMINAL
            ///TUBE LENGTH AND SPEED OF SOUND
            var speedOfSound = WaveHelper.SpeedOfSound(parameters.TubeTemp);

            _controlPeriod = Math.Round((speedOfSound * parameters.TotalSections * parameters.SectionLength) / (parameters.TubeLength * parameters.InControlRate));
            SampleRate     = parameters.InControlRate * _controlPeriod;
            //var actualTubeLength = (speedOfSound * totalSections * sectionLength) / sampleRate;
            _nyquist           = SampleRate / 2.0;
            _breathinessFactor = parameters.GlotSrcBreathiness / 100d;
            _crossmixFactor    = 1.0d / WaveHelper.CalculateAmplitude(parameters.NoiseCrossmixOffset);
            _dampingFactor     = (1.0d - (parameters.JunctionLoss / 100d));
            _waveTable         = new WaveTable(SampleRate, parameters);
            _bandpassFilter    = new BandpassFilter();
            _vocalTract        = new VocalTract(parameters, _nyquist, SampleRate);
            // initializeFIRFilter(FIR_BETA, FIR_GAMMA, FIR_CUTOFF);
            // /*  INITIALIZE THE SAMPLE RATE CONVERSION ROUTINES  */
            // initializeConversion();

            /*  INITIALIZE THE TEMPORARY OUTPUT FILE  */
            // tempFilePtr = tmpfile();
            // rewind(tempFilePtr);
        }
Exemplo n.º 3
0
        public SampleDataAsset(string name, WaveFile wave)
        {
            if (name == null)
            {
                throw new ArgumentNullException("An asset must be given a valid name.");
            }
            assetName = name;
            SamplerChunk smpl = wave.FindChunk <SamplerChunk>();

            if (smpl != null)
            {
                sampleRate = (int)(44100.0 * (1.0 / (smpl.SamplePeriod / 22675.0)));
                rootKey    = (short)smpl.UnityNote;
                tune       = (short)(smpl.PitchFraction * 100);
                if (smpl.Loops.Length > 0)
                {
                    //--WARNING ASSUMES: smpl.Loops[0].Type == SamplerChunk.SampleLoop.LoopType.Forward
                    loopStart = smpl.Loops[0].Start;
                    loopEnd   = smpl.Loops[0].End + smpl.Loops[0].Fraction + 1;
                }
            }
            else
            {
                sampleRate = wave.Format.SampleRate;
            }
            byte[] data = wave.Data.RawSampleData;
            if (wave.Format.ChannelCount != audioChannels) //reformat to supported channels
            {
                data = WaveHelper.GetChannelPcmData(data, wave.Format.BitsPerSample, wave.Format.ChannelCount, audioChannels);
            }
            sampleData = PcmData.Create(wave.Format.BitsPerSample, data, true);
            start      = 0;
            end        = sampleData.Length;
        }
Exemplo n.º 4
0
        public void SetFricationTaps(IntonationModel model)
        {
            double fricationAmplitude = WaveHelper.CalculateAmplitude(model.FricationVolume);


            /*  CALCULATE POSITION REMAINDER AND COMPLEMENT  */
            var integerPart = (int)model.FricationPosition;
            var complement  = model.FricationPosition - (double)integerPart;
            var remainder   = 1.0 - complement;

            /*  SET THE FRICATION TAPS  */
            for (int i = 0; i < 8; i++)
            {
                if (i == integerPart)
                {
                    FricationTap[i] = remainder * fricationAmplitude;
                    if ((i + 1) < 8)
                    {
                        FricationTap[++i] = complement * fricationAmplitude;
                    }
                }
                else
                {
                    FricationTap[i] = 0.0;
                }
            }
        }
Exemplo n.º 5
0
        private void InitializeThroat(ThroatModelParameters parameters, double sampleRate)
        {
            ta0 = (parameters.ThroatFreqCutoff * 2.0) / sampleRate;
            tb1 = 1.0 - ta0;

            throatGain = WaveHelper.CalculateAmplitude(parameters.ThroatVolume);
        }
Exemplo n.º 6
0
        public SampleDataAsset(string name, WaveFile wave)
        {
            if (name == null)
            {
                throw new ArgumentNullException("An asset must be given a valid name.");
            }
            assetName = name;
            SamplerChunk smpl = wave.FindChunk <SamplerChunk>();

            if (smpl != null)
            {
                sampleRate = (int)(44100.0 * (1.0 / (smpl.SamplePeriod / 22675.0)));
                rootKey    = (short)smpl.UnityNote;
                tune       = (short)(smpl.PitchFraction * 100);
                if (smpl.Loops.Length > 0)
                {
                    CrossPlatformHelper.Assert(smpl.Loops[0].Type == SamplerChunk.SampleLoop.LoopType.Forward, "Warning: Unsupported LoopType in " + assetName);
                    loopStart = smpl.Loops[0].Start;
                    loopEnd   = smpl.Loops[0].End + smpl.Loops[0].Fraction + 1;
                }
            }
            else
            {
                sampleRate = wave.Format.SampleRate;
            }
            sampleData = WaveHelper.GetSampleData(wave, audioChannels);
            start      = 0;
            end        = sampleData.Length;
        }
Exemplo n.º 7
0
 public IChunk GetChunk(WaveHelper.WaveChunkType ChunkType)
 {
     for (int x = 0; x < waveChunks.Length; x++)
     {
         if (waveChunks[x].GetChunkType() == ChunkType)
             return waveChunks[x];
     }
     return null;
 }
Exemplo n.º 8
0
 public IChunk GetChunk(int startIndex, WaveHelper.WaveChunkType ChunkType)
 {
     if (startIndex >= waveChunks.Length)
         return null;
     for (int x = startIndex; x < waveChunks.Length; x++)
     {
         if (waveChunks[x].GetChunkType() == ChunkType)
             return waveChunks[x];
     }
     return null;
 }
Exemplo n.º 9
0
 void InctantiateCube()
 {
     for (int i = 0; i < Height; i++)
     {
         for (int j = 0; j < Width; j++)
         {
             WaveHelper go = (Instantiate(Cube, new Vector3(i, 0, j), new Quaternion()) as GameObject).GetComponent <WaveHelper>();
             go.gameObject.GetComponent <MeshRenderer>().material = CubeMaterials[Random.Range(0, CubeMaterials.Count)];
             Cubes.Add(go);
         }
     }
 }
Exemplo n.º 10
0
        public SampleDataAsset(int size, BinaryReader reader)
        {
            assetName  = IOHelper.Read8BitString(reader, 20);
            sampleRate = reader.ReadInt32();
            rootKey    = reader.ReadInt16();
            tune       = reader.ReadInt16();
            loopStart  = reader.ReadDouble();
            loopEnd    = reader.ReadDouble();
            byte bits  = reader.ReadByte();
            byte chans = reader.ReadByte(); //only 1 is supported

            sampleData = WaveHelper.GetMonoSampleData(reader.ReadBytes(size - 46), bits, chans);
            start      = 0;
            end        = sampleData.Length;
        }
Exemplo n.º 11
0
        public SampleDataAsset(int size, BinaryReader reader)
        {
            assetName  = IOHelper.Read8BitString(reader, 20);
            sampleRate = reader.ReadInt32();
            rootKey    = reader.ReadInt16();
            tune       = reader.ReadInt16();
            loopStart  = reader.ReadDouble();
            loopEnd    = reader.ReadDouble();
            byte bits  = reader.ReadByte();
            byte chans = reader.ReadByte();

            byte[] data = reader.ReadBytes(size - 46);
            if (chans != audioChannels) //reformat to supported channels
            {
                data = WaveHelper.GetChannelPcmData(data, bits, chans, audioChannels);
            }
            sampleData = PcmData.Create(bits, data, true);
            start      = 0;
            end        = sampleData.Length;
        }
Exemplo n.º 12
0
        private void ParseInstrumentData(string[] text, string InstrumentPath, InstrumentBank bank)
        {
            allSamplesHaveDualChannels = true;
            SfzRegion Group = new SfzRegion();

            bool[]           groupValues = new bool[21];
            List <Sample>    Samples     = new List <Sample>();
            List <string>    SampleNames = new List <string>();
            List <SfzRegion> Regions     = new List <SfzRegion>();

            for (int x = 0; x < text.Length; x++)
            {
                string line = text[x].Trim();
                if (line != "")
                {
                    switch (line.Substring(0, 1).ToLower())
                    {
                    case "<":
                        if (line == "<group>")
                        {
                            while (text[x] != "")
                            {
                                x++;
                                if (x >= text.Length)
                                {
                                    break;
                                }
                                string[] Rvalue = text[x].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                                if (Rvalue.Length == 2)
                                {
                                    switch (Rvalue[0])
                                    {
                                    case "sample":
                                        Rvalue[1]      = System.IO.Path.GetFileNameWithoutExtension(Rvalue[1]);
                                        groupValues[0] = true;
                                        if (SampleNames.Contains(Rvalue[1]))        //its in the local list so no need to check the master list
                                        {
                                            Group.SampleIndex = SampleNames.IndexOf(Rvalue[1]);
                                        }
                                        else
                                        {
                                            SampleNames.Add(Rvalue[1]);
                                            //check if the sample is in the master list.
                                            int    sampNum = bank.SampleNameList.IndexOf(Rvalue[1]);
                                            Sample s;
                                            if (sampNum > -1)        //its in the list
                                            {
                                                s = bank.SampleList[sampNum];
                                            }
                                            else        //its not in the list
                                            {
                                                s = new Sample(InstrumentPath + "SAMPLES/" + Rvalue[1] + ".wav");
                                                bank.SampleList.Add(s);
                                                bank.SampleNameList.Add(Rvalue[1]);
                                            }
                                            if (s.isDualChannel == false)
                                            {
                                                allSamplesHaveDualChannels = false;
                                            }
                                            Samples.Add(s);
                                            Group.SampleIndex = SampleNames.Count - 1;
                                        }
                                        break;

                                    case "hikey":
                                        Group.HiNote   = (byte)int.Parse(Rvalue[1]);
                                        groupValues[1] = true;
                                        break;

                                    case "lokey":
                                        Group.LoNote   = (byte)int.Parse(Rvalue[1]);
                                        groupValues[2] = true;
                                        break;

                                    case "loop_start":
                                        Group.LoopStart = int.Parse(Rvalue[1]);
                                        groupValues[3]  = true;
                                        break;

                                    case "loop_end":
                                        Group.LoopEnd  = int.Parse(Rvalue[1]);
                                        groupValues[4] = true;
                                        break;

                                    case "tune":
                                        Group.Tune     = int.Parse(Rvalue[1]) / 100.0f;
                                        groupValues[5] = true;
                                        break;

                                    case "pitch_keycenter":
                                        Group.Root     = int.Parse(Rvalue[1]);
                                        groupValues[6] = true;
                                        break;

                                    case "volume":
                                        Group.Volume   = float.Parse(Rvalue[1]);
                                        groupValues[7] = true;
                                        break;

                                    case "loop_mode":
                                        groupValues[8] = true;
                                        switch (Rvalue[1])
                                        {
                                        case "loop_continuous":
                                            Group.LoopMode = 1;
                                            break;

                                        case "loop_sustain":
                                            Group.LoopMode = 2;
                                            break;

                                        default:
                                            Group.LoopMode = 0;
                                            break;
                                        }
                                        break;

                                    case "ampeg_release":
                                        groupValues[9] = true;
                                        Group.Release  = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_attack":
                                        groupValues[10] = true;
                                        Group.Attack    = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_decay":
                                        groupValues[11] = true;
                                        Group.Decay     = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_hold":
                                        groupValues[12] = true;
                                        Group.Hold      = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "lovel":
                                        groupValues[13]  = true;
                                        Group.LoVelocity = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "hivel":
                                        groupValues[14]  = true;
                                        Group.HiVelocity = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "lochan":
                                        groupValues[15] = true;
                                        Group.LoChannel = (byte)(int.Parse(Rvalue[1]) - 1);
                                        break;

                                    case "hichan":
                                        groupValues[16] = true;
                                        Group.HiChannel = (byte)(int.Parse(Rvalue[1]) - 1);
                                        break;

                                    case "key":
                                        Group.Root     = int.Parse(Rvalue[1]);
                                        groupValues[6] = true;
                                        Group.HiNote   = (byte)Group.Root;
                                        groupValues[1] = true;
                                        Group.LoNote   = (byte)Group.Root;
                                        groupValues[2] = true;
                                        break;

                                    case "offset":
                                        groupValues[17] = true;
                                        Group.Offset    = int.Parse(Rvalue[1]);
                                        break;

                                    case "pan":
                                        groupValues[18] = true;
                                        Group.Pan       = float.Parse(Rvalue[1]);
                                        break;

                                    case "effect1":
                                        groupValues[19] = true;
                                        Group.Effect1   = float.Parse(Rvalue[1]);
                                        break;

                                    case "effect2":
                                        groupValues[20] = true;
                                        Group.Effect2   = float.Parse(Rvalue[1]);
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                        }
                        else if (line == "<region>")
                        {
                            SfzRegion r = new SfzRegion();
                            while (text[x] != "")
                            {
                                x++;
                                if (x >= text.Length)
                                {
                                    break;
                                }
                                string[] Rvalue = text[x].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                                if (Rvalue.Length == 2)
                                {
                                    switch (Rvalue[0])
                                    {
                                    case "sample":
                                        Rvalue[1] = System.IO.Path.GetFileNameWithoutExtension(Rvalue[1]); //remove ending .wav
                                        if (SampleNames.Contains(Rvalue[1]))                               //its in the local list so no need to check the master list
                                        {
                                            r.SampleIndex = SampleNames.IndexOf(Rvalue[1]);
                                        }
                                        else
                                        {
                                            SampleNames.Add(Rvalue[1]);
                                            //check if the sample is in the master list.
                                            int    sampNum = bank.SampleNameList.IndexOf(Rvalue[1]);
                                            Sample s;
                                            if (sampNum > -1)        //its in the list
                                            {
                                                s = bank.SampleList[sampNum];
                                            }
                                            else        //its not in the list
                                            {
                                                s = new Sample(InstrumentPath + "SAMPLES/" + Rvalue[1] + ".wav");
                                                bank.SampleList.Add(s);
                                                bank.SampleNameList.Add(Rvalue[1]);
                                            }
                                            if (s.isDualChannel == false)
                                            {
                                                allSamplesHaveDualChannels = false;
                                            }
                                            Samples.Add(s);
                                            r.SampleIndex = SampleNames.Count - 1;
                                        }
                                        break;

                                    case "hikey":
                                        r.HiNote = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "lokey":
                                        r.LoNote = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "loop_start":
                                        r.LoopStart = int.Parse(Rvalue[1]);
                                        break;

                                    case "loop_end":
                                        r.LoopEnd = int.Parse(Rvalue[1]);
                                        break;

                                    case "tune":
                                        r.Tune = int.Parse(Rvalue[1]) / 100.0f;
                                        break;

                                    case "pitch_keycenter":
                                        r.Root = int.Parse(Rvalue[1]);
                                        break;

                                    case "volume":
                                        r.Volume = float.Parse(Rvalue[1]);
                                        break;

                                    case "loop_mode":
                                        switch (Rvalue[1])
                                        {
                                        case "loop_continuous":
                                            r.LoopMode = 1;
                                            break;

                                        case "loop_sustain":
                                            r.LoopMode = 2;
                                            break;

                                        default:
                                            r.LoopMode = 0;
                                            break;
                                        }
                                        break;

                                    case "ampeg_release":
                                        r.Release = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_attack":
                                        r.Attack = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_decay":
                                        r.Decay = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "ampeg_hold":
                                        r.Hold = SynthHelper.getSampleFromTime(this.SampleRate, float.Parse(Rvalue[1]));
                                        break;

                                    case "lovel":
                                        r.LoVelocity = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "hivel":
                                        r.HiVelocity = (byte)int.Parse(Rvalue[1]);
                                        break;

                                    case "lochan":
                                        r.LoChannel = (byte)(int.Parse(Rvalue[1]) - 1);
                                        break;

                                    case "hichan":
                                        r.HiChannel = (byte)(int.Parse(Rvalue[1]) - 1);
                                        break;

                                    case "key":
                                        r.Root   = int.Parse(Rvalue[1]);
                                        r.HiNote = (byte)r.Root;
                                        r.LoNote = (byte)r.Root;
                                        break;

                                    case "offset":
                                        r.Offset = int.Parse(Rvalue[1]);
                                        break;

                                    case "pan":
                                        r.Pan = float.Parse(Rvalue[1]);
                                        break;

                                    case "effect1":
                                        r.Effect1 = float.Parse(Rvalue[1]);
                                        break;

                                    case "effect2":
                                        r.Effect2 = float.Parse(Rvalue[1]);
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                            if (Regions.Contains(r) == false)
                            {
                                Regions.Add(r);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            //Apply group values here
            for (int x = 0; x < Regions.Count; x++)
            {
                if (groupValues[0] == true)
                {
                    Regions[x].SampleIndex = Group.SampleIndex;
                }
                if (groupValues[1] == true)
                {
                    Regions[x].HiNote = Group.HiNote;
                }
                if (groupValues[2] == true)
                {
                    Regions[x].LoNote = Group.LoNote;
                }
                if (groupValues[3] == true)
                {
                    Regions[x].LoopStart = Group.LoopStart;
                }
                if (groupValues[4] == true)
                {
                    Regions[x].LoopEnd = Group.LoopEnd;
                }
                if (groupValues[5] == true)
                {
                    Regions[x].Tune = Group.Tune;
                }
                if (groupValues[6] == true)
                {
                    Regions[x].Root = Group.Root;
                }
                if (groupValues[7] == true)
                {
                    Regions[x].Volume = Group.Volume;
                }
                if (groupValues[8] == true)
                {
                    Regions[x].LoopMode = Group.LoopMode;
                }
                if (groupValues[9] == true)
                {
                    Regions[x].Release = Group.Release;
                }
                if (groupValues[10] == true)
                {
                    Regions[x].Attack = Group.Attack;
                }
                if (groupValues[11] == true)
                {
                    Regions[x].Decay = Group.Decay;
                }
                if (groupValues[12] == true)
                {
                    Regions[x].Hold = Group.Hold;
                }
                if (groupValues[13] == true)
                {
                    Regions[x].LoVelocity = Group.LoVelocity;
                }
                if (groupValues[14] == true)
                {
                    Regions[x].HiVelocity = Group.HiVelocity;
                }
                if (groupValues[15] == true)
                {
                    Regions[x].LoChannel = Group.LoChannel;
                }
                if (groupValues[16] == true)
                {
                    Regions[x].HiChannel = Group.HiChannel;
                }
                if (groupValues[17] == true)
                {
                    Regions[x].Offset = Group.Offset;
                }
                if (groupValues[18] == true)
                {
                    Regions[x].Pan = Group.Pan;
                }
                if (groupValues[19] == true)
                {
                    Regions[x].Effect1 = Group.Effect1;
                }
                if (groupValues[20] == true)
                {
                    Regions[x].Effect2 = Group.Effect2;
                }
            }
            base.SampleList = Samples.ToArray();
            regions         = Regions.ToArray();
            //Fix parameters so enforce isn't needed
            for (int x = 0; x < regions.Length; x++)
            {
                Sample s      = base.SampleList[regions[x].SampleIndex];
                float  factor = (this.SampleRate / (float)s.OriginalSampleRate);
                regions[x].LoopEnd   = (int)(regions[x].LoopEnd * factor);
                regions[x].LoopStart = (int)(regions[x].LoopStart * factor);
                regions[x].Offset    = (int)(regions[x].Offset * factor);
                //Set loopend to end of sample if none is provided
                if (regions[x].LoopEnd <= 0)
                {
                    if (this.SampleRate != s.SampleRate)
                    {
                        regions[x].LoopEnd = (int)(base.SampleList[regions[x].SampleIndex].SamplesPerChannel * factor) - 1;
                    }
                    else
                    {
                        regions[x].LoopEnd = (int)(base.SampleList[regions[x].SampleIndex].SamplesPerChannel) - 1;
                    }
                }
            }
            //Resample as well
            for (int x = 0; x < base.SampleList.Length; x++)
            {
                Sample s      = base.SampleList[x];
                float  factor = this.SampleRate / (float)s.SampleRate;
                if (factor != 1.0f)
                {
                    s.setAllSampleData(WaveHelper.ReSample(this.SampleRate, s.SampleRate, s.getAllSampleData()));
                    s.SampleRate = this.SampleRate;
                }
            }
        }
Exemplo n.º 13
0
        public static string WriteWaveFile(IEnumerable <double> waveform, double sampleRate, ThroatModelParameters parameters)
        {
            var outputScale = 0.25;
            var rangeMax    = 32767d;
            var balance     = 0;

            // var sampleRateRatio = outputRate / sampleRate;
            var(fileStream, path) = CreateFileStream();
            //WriteWaveFileHeader(2, waveform.Count(), parameters.OutputSampleRate, fileStream);
            WriteWaveFileHeader(2, waveform.Count(), sampleRate, fileStream);
            var scale      = outputScale * (rangeMax / waveform.Max()) * WaveHelper.CalculateAmplitude(parameters.MasterVolume);
            var leftScale  = -((balance / 2.0) - 0.5) * scale * 2.0;
            var rightScale = ((balance / 2.0) + 0.5) * scale * 2.0;

            foreach (var data in waveform)
            {
                WriteShort(fileStream, (short)Math.Round(data * leftScale));
                WriteShort(fileStream, (short)Math.Round(data * rightScale));
            }
            fileStream.Flush();
            fileStream.Close();
            return(path);
            // int endPtr;
            // /*  CALCULATE END POINTER  */
            // endPtr = fillPtr - padSize;

            // /*  ADJUST THE END POINTER, IF LESS THAN ZERO  */
            // if (endPtr < 0)
            //   endPtr += BUFFER_SIZE;

            // /*  ADJUST THE ENDPOINT, IF LESS THEN THE EMPTY POINTER  */
            // if (endPtr < emptyPtr)
            //   endPtr += BUFFER_SIZE;

            /*  UPSAMPLE LOOP (SLIGHTLY MORE EFFICIENT THAN DOWNSAMPLING)  */
            // if (sampleRateRatio >= 1.0) {
            //   while (emptyPtr < endPtr) {
            //     int index;
            //     unsigned int filterIndex;
            //     double output, interpolation, absoluteSampleValue;

            //     /*  RESET ACCUMULATOR TO ZERO  */
            //     output = 0.0;

            //     /*  CALCULATE INTERPOLATION VALUE (STATIC WHEN UPSAMPLING)  */
            //     interpolation = (double)mValue(timeRegister) / (double)M_RANGE;

            //     /*  COMPUTE THE LEFT SIDE OF THE FILTER CONVOLUTION  */
            //     index = emptyPtr;
            //     for (filterIndex = lValue(timeRegister); filterIndex < FILTER_LENGTH; srDecrement(&index,BUFFER_SIZE), filterIndex += filterIncrement) {
            //       output += (buffer[index] * (h[filterIndex] + (deltaH[filterIndex] * interpolation)));
            //     }

            //     /*  ADJUST VALUES FOR RIGHT SIDE CALCULATION  */
            //     timeRegister = ~timeRegister;
            //     interpolation = (double)mValue(timeRegister) / (double)M_RANGE;

            //     /*  COMPUTE THE RIGHT SIDE OF THE FILTER CONVOLUTION  */
            //     index = emptyPtr;
            //     srIncrement(&index,BUFFER_SIZE);
            //     for (filterIndex = lValue(timeRegister); filterIndex < FILTER_LENGTH; srIncrement(&index,BUFFER_SIZE), filterIndex += filterIncrement) {
            //       output += (buffer[index] * (h[filterIndex] + (deltaH[filterIndex] * interpolation)));
            //     }

            //     /*  RECORD MAXIMUM SAMPLE VALUE  */
            //     absoluteSampleValue = fabs(output);
            //     if (absoluteSampleValue > maximumSampleValue)
            //       maximumSampleValue = absoluteSampleValue;

            //     /*  INCREMENT SAMPLE NUMBER  */
            //     numberSamples++;

            //     /*  OUTPUT THE SAMPLE TO THE TEMPORARY FILE  */
            //     fwrite((char *)&output, sizeof(output), 1, tempFilePtr);

            //     /*  CHANGE TIME REGISTER BACK TO ORIGINAL FORM  */
            //     timeRegister = ~timeRegister;

            //     /*  INCREMENT THE TIME REGISTER  */
            //     timeRegister += timeRegisterIncrement;

            //     /*  INCREMENT THE EMPTY POINTER, ADJUSTING IT AND END POINTER  */
            //     emptyPtr += nValue(timeRegister);

            //     if (emptyPtr >= BUFFER_SIZE) {
            //       emptyPtr -= BUFFER_SIZE;
            //       endPtr -= BUFFER_SIZE;
            //     }

            //     /*  CLEAR N PART OF TIME REGISTER  */
            //     timeRegister &= (~N_MASK);
            //   }
            // }
            // else { /*  DOWNSAMPLING CONVERSION LOOP  */
            //   while (emptyPtr < endPtr) {
            //     int index;
            //     unsigned int phaseIndex, impulseIndex;
            //     double absoluteSampleValue, output, impulse;

            //     /*  RESET ACCUMULATOR TO ZERO  */
            //     output = 0.0;

            //     /*  COMPUTE P PRIME  */
            //     phaseIndex = (unsigned int)rint(((double)fractionValue(timeRegister)) * sampleRateRatio);

            //     /*  COMPUTE THE LEFT SIDE OF THE FILTER CONVOLUTION  */
            //     index = emptyPtr;
            //     while ((impulseIndex = (phaseIndex>>M_BITS)) < FILTER_LENGTH) {
            //       impulse = h[impulseIndex] + (deltaH[impulseIndex] * (((double)mValue(phaseIndex)) / (double)M_RANGE));
            //       output += (buffer[index] * impulse);
            //       srDecrement(&index,BUFFER_SIZE);
            //       phaseIndex += phaseIncrement;
            //     }

            //     /*  COMPUTE P PRIME, ADJUSTED FOR RIGHT SIDE  */
            //     phaseIndex = (unsigned int)rint(((double)fractionValue(~timeRegister)) * sampleRateRatio);

            //     /*  COMPUTE THE RIGHT SIDE OF THE FILTER CONVOLUTION  */
            //     index = emptyPtr;
            //     srIncrement(&index,BUFFER_SIZE);
            //     while ((impulseIndex = (phaseIndex>>M_BITS)) < FILTER_LENGTH) {
            //       impulse = h[impulseIndex] + (deltaH[impulseIndex] * (((double)mValue(phaseIndex)) / (double)M_RANGE));
            //       output += (buffer[index] * impulse);
            //       srIncrement(&index,BUFFER_SIZE);
            //       phaseIndex += phaseIncrement;
            //     }

            //     /*  RECORD MAXIMUM SAMPLE VALUE  */
            //     absoluteSampleValue = fabs(output);
            //     if (absoluteSampleValue > maximumSampleValue)
            //       maximumSampleValue = absoluteSampleValue;

            //     /*  INCREMENT SAMPLE NUMBER  */
            //     numberSamples++;

            //     /*  OUTPUT THE SAMPLE TO THE TEMPORARY FILE  */
            //     fwrite((char *)&output, sizeof(output), 1, tempFilePtr);

            //     /*  INCREMENT THE TIME REGISTER  */
            //     timeRegister += timeRegisterIncrement;

            //     /*  INCREMENT THE EMPTY POINTER, ADJUSTING IT AND END POINTER  */
            //     emptyPtr += nValue(timeRegister);
            //     if (emptyPtr >= BUFFER_SIZE) {
            //       emptyPtr -= BUFFER_SIZE;
            //       endPtr -= BUFFER_SIZE;
            //     }

            //     /*  CLEAR N PART OF TIME REGISTER  */
            //     timeRegister &= (~N_MASK);
            //   }
            // }
        }
        public IEnumerable <double> Synthesize(IEnumerable <IntonationModel> model)
        {
            double frequency, amplitude, aspVol, pulse, noise, pulsedNoise, signal, crossmix;

            /*  CONTROL RATE LOOP  */
            foreach (var modelSection in model)
            {
                /*  SAMPLE RATE LOOP  */
                for (int i = 0; i < _controlPeriod; i++)
                {
                    /*  CONVERT PARAMETERS HERE  */
                    frequency = WaveHelper.CaclulateFrequency(modelSection.Pitch);
                    amplitude = WaveHelper.CalculateAmplitude(modelSection.GlotVolume);
                    aspVol    = WaveHelper.CalculateAmplitude(modelSection.AspirationVolume);
                    _vocalTract.CalculateTubeCoefficients(modelSection, _parameters);
                    _vocalTract.SetFricationTaps(modelSection);
                    _bandpassFilter.Update(modelSection.FricationBW, modelSection.FricationCF, SampleRate);

                    noise = WaveHelper.Noise();

                    /*  UPDATE THE SHAPE OF THE GLOTTAL PULSE, IF NECESSARY  */
                    if (_parameters.GlotSrcType == GlotSrcType.Pulse)
                    {
                        _waveTable.UpdateWavetable(amplitude);
                    }

                    /*  CREATE GLOTTAL PULSE (OR SINE TONE)  */
                    pulse = _waveTable.Oscillator(frequency);

                    /*  CREATE PULSED NOISE  */
                    pulsedNoise = noise * pulse;

                    /*  CREATE NOISY GLOTTAL PULSE  */
                    pulse = amplitude * ((pulse * (1.0 - _breathinessFactor)) + (pulsedNoise * _breathinessFactor));

                    /*  CROSS-MIX PURE NOISE WITH PULSED NOISE  */
                    if (_parameters.PulseModulationOfNoise)
                    {
                        crossmix = amplitude * _crossmixFactor;
                        crossmix = (crossmix < 1.0) ? crossmix : 1.0;
                        signal   = (pulsedNoise * crossmix) + (noise * (1.0 - crossmix));
                    }
                    else
                    {
                        signal = noise;
                    }

                    /*  PUT SIGNAL THROUGH VOCAL TRACT  */
                    signal = _vocalTract.CalculateVocalTractSignal(((pulse + (aspVol * signal)) * _parameters.VocalTractScale), _bandpassFilter.Filter(signal), _dampingFactor);
                    /*  PUT PULSE THROUGH THROAT  */
                    signal += _vocalTract.CalculateThroatSignal(pulse * _parameters.VocalTractScale);

                    // /*  OUTPUT SAMPLE HERE  */
                    // dataFill(signal);
                    yield return(signal);

                    // /*  DO SAMPLE RATE INTERPOLATION OF CONTROL PARAMETERS  */
                    // sampleRateInterpolation();
                }
            }
            yield break;
        }
Exemplo n.º 15
0
 // private RegularEnemyManagerType1 _enemyManager1;
 // private RegularEnemyManagerType2 _enemyManager2;
 public GameState(Game1 game, ContentManager content) : base(game, content)
 {
     waveHelper = new WaveHelper(content);
 }
Exemplo n.º 16
0
 public MenuState(Game1 game, ContentManager content) : base(game, content)
 {
     var t = new WaveHelper(content);
 }