public void LoadVoiceSample(string speakerName, byte[] sample) { if (string.IsNullOrEmpty(speakerName)) { throw new ArgumentException(nameof(speakerName)); } if (sample == null || sample.Length == 0) { throw new ArgumentException(nameof(sample)); } var speakerToUpdate = Speakers.FirstOrDefault(x => x.Name == speakerName); var voiceSample = new VoiceSample(sample, _sampleRate); if (speakerToUpdate != null) { for (int i = 0; i < speakerToUpdate.Samples[0].MelFrequency.Length; i++) { speakerToUpdate.Samples[0].MelFrequency[i] = (speakerToUpdate.Samples[0].MelFrequency[i] + voiceSample.MelFrequency[i]) / 2; } } else { var speaker = new Speaker(speakerName); speaker.Id = Guid.NewGuid().ToString(); speaker.Samples.Add(voiceSample); Speakers.Add(speaker); } }
public void Construct_With_Mel_Frequency_Array_Length_Is_Lower_Than_Expected_Throws_Exception() { //arrange var melFrequency = new double[0]; //act Action action = () => { var sut = new VoiceSample(melFrequency); }; //assert action.Should() .Throw <ArgumentException>(); }
public void Construct_With_Mel_Frequency_Parameters_Sucessfully() { //arrange var melFrequency = new double[20]; //act Action action = () => { var sut = new VoiceSample(melFrequency); }; //assert action.Should() .NotThrow <ArgumentException>(); }
public void Construct_With_Correct_Parameters_Sucessfully() { //arrange byte[] sampleData = File.ReadAllBytes("test.wav"); var sampleRate = 44100; //act Action action = () => { var sut = new VoiceSample(sampleData, sampleRate); }; //assert action.Should() .NotThrow <Exception>(); }
public void Construct_Sample_Rate_Is_Negative_Throws_Exception() { //arrange byte[] sampleData = new byte[1]; var sampleRate = -1; //act Action action = () => { var sut = new VoiceSample(sampleData, sampleRate); }; //assert action.Should() .Throw <ArgumentException>(); }
public void Construct_Sample_Data_Is_Null_Throws_Exception() { //arrange byte[] sampleData = null; var sampleRate = 1; //act Action action = () => { var sut = new VoiceSample(sampleData, sampleRate); }; //assert action.Should() .Throw <ArgumentNullException>(); }
public string IdentifySpeaker(byte[] sampleToIdentify) { var sample = new VoiceSample(sampleToIdentify, _sampleRate); var speakersDistances = Speakers.Select(x => { double distance = 0; for (int i = 0; i < x.Samples[0].MelFrequency.Length; i++) { distance += Math.Abs(x.Samples[0].MelFrequency[i] - sample.MelFrequency[i]); } return(new { Speaker = x.Name, Distance = distance }); }); return(speakersDistances.OrderBy(x => x.Distance).First().Speaker); }
public void Construct_Calculates_Expected_Mel_Frequency_Coeficients_Sucessfully() { byte[] sampleData = File.ReadAllBytes("test.wav"); var sampleRate = 44100; //act var sut = new VoiceSample(sampleData, sampleRate); //assert sut.MelFrequency .Should() .NotBeNull(); sut.MelFrequency .Should() .NotBeEmpty(); sut.MelFrequency .Should() .BeEquivalentTo(_expectedMel); }
public void InitializeCore(List <SpeakerModel> speakersData) { if (speakersData == null || speakersData.Count == 0) { throw new ArgumentException(nameof(speakersData)); } foreach (var speakerData in speakersData) { var speaker = new Speaker(speakerData.Name) { Id = speakerData.Id }; foreach (var dataSample in speakerData.Features) { var sample = new VoiceSample(dataSample.Features.Split(';').Select(x => double.Parse(x.Replace('.', ','))).ToArray()); sample.Id = dataSample.Id; speaker.Samples.Add(sample); } Speakers.Add(speaker); } }