/// <summary> /// Initializes default audio endpoint and attaches sound listener into it /// </summary> protected static void Init() { if (isInitted) { return; } lock (lockObj) { //Double checked locking if (isInitted) { return; } //Get default audio endpoint and attach sound listener var audioEndpoint = NAudioHelper.GetDefaultAudioEndpoint(); if (audioEndpoint != null) { NAudioHelper.AttachSoundLevelListener(audioEndpoint); } //Set isInitted to true to make sure the logic executes only once isInitted = true; } }
public async Task <double> GetMediaDurationInMinUsingMediaElement(string mediaFile) { tb1.Text = Title = $"{System.IO.Path.GetFileName(mediaFile)} "; var ttlMin = await getDurnUsingMediaElnt(mediaFile); for (int j = 20; j > 0 && ttlMin == ConstHelper.Unknown4004Duration; j--) { await Task.Delay(3000); await Application.Current.Dispatcher.BeginInvoke(new Action(async() => { ttlMin = await getDurnUsingMediaElnt(mediaFile); })); tb1.Text = Title += $"{j} "; } if (ttlMin == ConstHelper.Unknown4004Duration || ttlMin > 300) { ttlMin = NAudioHelper.GetDuration(mediaFile).TotalMinutes; } tb1.Text = Title += $" => {ttlMin:N1}min."; tb1.Foreground = new SolidColorBrush((ttlMin == ConstHelper.Unknown4004Duration || ttlMin > 300) ? Colors.Red : Colors.Green); return(ttlMin); }
private MemoryStream GetResolutionNoDO(string doNoteName, ResolutionType resolutionType) { double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNotemillis = (60 / bpm) * 1000; TimeSpan quarterNoteDuration = TimeSpan.FromMilliseconds(quarterNotemillis); TimeSpan halfNoteDuration = quarterNoteDuration.Add(quarterNoteDuration); TimeSpan wholeNoteDuration = halfNoteDuration.Add(halfNoteDuration); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1; ISampleProvider note2; switch (resolutionType) { case ResolutionType.DoDoReDo: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; case ResolutionType.DoDoFaMi: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, wholeNoteDuration); break; case ResolutionType.DoDoLaSo: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, wholeNoteDuration); break; case ResolutionType.DoDoTiHighDo: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + 12, wholeNoteDuration); break; case ResolutionType.DoDoLowTiDo: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.DownMinor2nd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; default: throw new NotSupportedException($"ResolutionType '{resolutionType}' is not supported."); } ISampleProvider phrase = note1 .FollowedBy(note2); SampleToWaveProvider stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
private MixingSampleProvider CreateMajorTriad(string doNoteName, string chordAndInversion, string movement) { _log.Debug($"doNoteName: {doNoteName}, chordAndInversion: {chordAndInversion}, movement: {movement ?? "null"}"); double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; double halfNoteMillis = quarterNoteMillis * 2; TimeSpan noteDuration = TimeSpan.FromMilliseconds(halfNoteMillis); string doNoteRegisterString = Regex.Replace(doNoteName, "[A-G#b]", ""); int doNoteRegister = int.Parse(doNoteRegisterString); string[] parts = chordAndInversion.Trim().Split(' '); string chord = parts[0].Trim(); string inversion = parts[1].Trim(); InversionType inversionType; switch (inversion) { case "(root)": inversionType = InversionType.Root; break; case "(1st)": inversionType = InversionType.LowFirst; break; case "(2nd)": inversionType = InversionType.LowSecond; break; default: inversionType = InversionType.Root; break; } string thisChordRootNote = GetClosestNote(doNoteName, chord); //thisChordRootNote = chord + doNoteRegister.ToString(); _log.Debug($"doNoteName: {doNoteName}, thisChordRootNote: {thisChordRootNote}"); string doFileName = NAudioHelper.GetFileNameFromNoteName(thisChordRootNote); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider[] samples; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); MixingSampleProvider msp = new MixingSampleProvider(samples[0].WaveFormat); msp.AddMixerInput(samples[0]); msp.AddMixerInput(samples[1]); msp.AddMixerInput(samples[2]); msp.AddMixerInput(samples[3]); return(msp); }
private MemoryStream GetHarmonicDrill(string doNoteName, L1C7HarmonicDrillType drillType) { TimeSpan duration = TimeSpan.FromSeconds(3); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2; switch (drillType) { // Minor 2nds. case L1C7HarmonicDrillType.MiFa2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, duration); break; case L1C7HarmonicDrillType.TiDo2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, duration); break; // Major 7ths. case L1C7HarmonicDrillType.DoTi7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); break; case L1C7HarmonicDrillType.FaMi7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, duration); break; default: throw new NotSupportedException($"L1C7HarmonicDrillType '{drillType}' is not supported."); } MixingSampleProvider msp = new MixingSampleProvider(note1.WaveFormat); msp.AddMixerInput(note1); msp.AddMixerInput(note2); var stwp = new SampleToWaveProvider(msp); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
public ActionResult GetNoteEx(string doNoteName, int type) { int doNoteNumber = NoteHelper.NoteNumberFromNoteName(doNoteName); int thisNoteNumber = doNoteNumber + type; ISampleProvider note = NAudioHelper.GetSampleProvider(thisNoteNumber, TimeSpan.FromSeconds(3)); var stwp = new SampleToWaveProvider(note); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
public ActionResult GetDOEx(string doNoteName) { _log.Info("Entered. Getting the note..."); ISampleProvider note = NAudioHelper.GetSampleProvider(doNoteName, TimeSpan.FromSeconds(2)); _log.Info("Got the note"); var stwp = new SampleToWaveProvider(note); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
public ActionResult GetPitchEx(string doNoteName, int type) { string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); int noteNumber = doNoteNumber + type; TimeSpan noteDuration = TimeSpan.FromSeconds(3); var note = NAudioHelper.GetSampleProvider(noteNumber, noteDuration); var stwp = new SampleToWaveProvider(note); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
public ActionResult DecreaseVolume(int level) { //Get default endpoint var audioEndpoint = NAudioHelper.GetDefaultAudioEndpoint(); if (audioEndpoint == null) { return(Json("No devices found to record")); } try { if (audioEndpoint.AudioEndpointVolume.MasterVolumeLevelScalar == 0) { return(Json("Device is already muted")); } //Set new volume and mute flag float newVolume = 0; if (level != 0) { newVolume = audioEndpoint.AudioEndpointVolume.MasterVolumeLevelScalar - (float)0.1; } if (newVolume < 0) { newVolume = 0; } audioEndpoint.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume; audioEndpoint.AudioEndpointVolume.Mute = newVolume == 0; } catch { //Swallow and return return(Json("An error has occured. WHAT DID YOU DO YOU????")); } return(Json(new { })); }
private MemoryStream GetMelodicInterval(string doNoteName, L1C7MelodicDrillType drillType) { double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; TimeSpan halfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2; switch (drillType) { #region Minor 2nds / minor 7ths // Minor 2nd intervals. case L1C7MelodicDrillType.MiFa2Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); break; case L1C7MelodicDrillType.TiDo2Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); break; case L1C7MelodicDrillType.FaMi2Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); break; case L1C7MelodicDrillType.DoTi2Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; // Major 7th intervals. case L1C7MelodicDrillType.DoTi7Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; case L1C7MelodicDrillType.FaMi7Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); break; case L1C7MelodicDrillType.TiDo7Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); break; case L1C7MelodicDrillType.MiFa7Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); break; #endregion Major 2nds / min 7ths default: throw new NotSupportedException($"L1C6MelodicDrillType '{drillType}' is not supported."); } var phrase = note1 .FollowedBy(note2); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
private MemoryStream GetMelodicDrill(string doNoteName, L1C7MelodicDrillType drillType) { double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; TimeSpan eighthNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis / 2); TimeSpan quarterNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis); TimeSpan dottedQuarterNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis).Add(eighthNoteDuration); TimeSpan halfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 2); TimeSpan halfNoteAndEighthNoteDuration = halfNoteDuration.Add(eighthNoteDuration); TimeSpan wholeNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 4); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2, note3, note4 = null; switch (drillType) { // Minor 2nd intervals. case L1C7MelodicDrillType.MiMiFaMiMin2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteAndEighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, wholeNoteDuration); break; case L1C7MelodicDrillType.TiDoDoMin2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.DownMinor2nd, dottedQuarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteAndEighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; case L1C7MelodicDrillType.FaFaMiMiMin2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteAndEighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, wholeNoteDuration); break; case L1C7MelodicDrillType.DoTiDoMin2nd: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, dottedQuarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.DownMinor2nd, halfNoteAndEighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; // Major 7th intervals. case L1C7MelodicDrillType.DoDoTiDoMaj7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteAndEighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C7MelodicDrillType.FaMiMiMaj7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, dottedQuarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteAndEighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, wholeNoteDuration); break; case L1C7MelodicDrillType.TiTiDoDoMaj7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteAndEighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; case L1C7MelodicDrillType.MiFaMiMaj7th: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, dottedQuarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteAndEighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, wholeNoteDuration); break; default: throw new NotSupportedException($"L1C7MelodicDrillType '{drillType}' is not supported."); } var phrase = note1 .FollowedBy(note2) .FollowedBy(note3); if (note4 != null) { phrase = phrase.FollowedBy(note4); } var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
public ActionResult Get4ChordProgression(string doNoteName, string progression) { TimeSpan noteDuration = TimeSpan.FromSeconds(2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); string[] chords = progression.Split('-'); ISampleProvider[] samples1, samples2, samples3, samples4; // First chord. string[] chordParts = chords[0].Split(' '); int degree = int.Parse(chordParts[0]); int newDoNoteNumber = GetDoNoteNumber(doNoteNumber, degree); samples1 = CreateTriad(newDoNoteNumber, degree, noteDuration); // Second chord. chordParts = chords[1].Split(' '); degree = int.Parse(chordParts[0]); newDoNoteNumber = GetDoNoteNumber(doNoteNumber, degree); samples2 = CreateTriad(newDoNoteNumber, degree, noteDuration); // Third chord. chordParts = chords[2].Split(' '); degree = int.Parse(chordParts[0]); newDoNoteNumber = GetDoNoteNumber(doNoteNumber, degree); samples3 = CreateTriad(newDoNoteNumber, degree, noteDuration); // Fourth chord. chordParts = chords[3].Split(' '); degree = int.Parse(chordParts[0]); newDoNoteNumber = GetDoNoteNumber(doNoteNumber, degree); samples4 = CreateTriad(newDoNoteNumber, degree, noteDuration); MixingSampleProvider msp1 = new MixingSampleProvider(samples1[0].WaveFormat); msp1.AddMixerInput(samples1[0]); msp1.AddMixerInput(samples1[1]); msp1.AddMixerInput(samples1[2]); MixingSampleProvider msp2 = new MixingSampleProvider(samples2[0].WaveFormat); msp2.AddMixerInput(samples2[0]); msp2.AddMixerInput(samples2[1]); msp2.AddMixerInput(samples2[2]); MixingSampleProvider msp3 = new MixingSampleProvider(samples3[0].WaveFormat); msp3.AddMixerInput(samples3[0]); msp3.AddMixerInput(samples3[1]); msp3.AddMixerInput(samples3[2]); MixingSampleProvider msp4 = new MixingSampleProvider(samples4[0].WaveFormat); msp4.AddMixerInput(samples4[0]); msp4.AddMixerInput(samples4[1]); msp4.AddMixerInput(samples4[2]); var phrase = msp1 .FollowedBy(msp2) .FollowedBy(msp3) .FollowedBy(msp4); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
public ActionResult Get3ChordProgressionEx(string doNoteName, int progressiontype) { var progressionType = (ProgressionType3)progressiontype; TimeSpan duration = TimeSpan.FromSeconds(2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider[] samples1; ISampleProvider[] samples2; ISampleProvider[] samples3; switch (progressionType) { case ProgressionType3.One2ndTwoMin1stThreeMin1st: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighSecond, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.TwoMinor, InversionType.HighFirst, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.ThreeMinor, InversionType.HighFirst, duration, true); break; case ProgressionType3.One1stFive2ndSixMin2nd: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighFirst, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowSecond, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.SixMinor, InversionType.LowSecond, duration, true); break; case ProgressionType3.Four2ndFive1stSixMin1st: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.LowSecond, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowFirst, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.SixMinor, InversionType.LowFirst, duration, true); break; case ProgressionType3.ThreeMin1stOne2ndTwoMin1st: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.ThreeMinor, InversionType.HighFirst, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighSecond, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.TwoMinor, InversionType.HighFirst, duration, true); break; case ProgressionType3.FourRootFive2ndThreeMinRoot: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.Root, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowSecond, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.ThreeMinor, InversionType.Root, duration, true); break; case ProgressionType3.OneRootTwoMin2ndFour1st: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.Root, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.TwoMinor, InversionType.LowSecond, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.LowFirst, duration, true); break; case ProgressionType3.One1stThreeMinRootTwoMinFirst: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighFirst, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.ThreeMinor, InversionType.Root, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.TwoMinor, InversionType.HighFirst, duration, true); break; case ProgressionType3.Four2ndOneRootFive1st: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.LowSecond, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.Root, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowFirst, duration, true); break; case ProgressionType3.SixMinRootFour1stOne2nd: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.SixMinor, InversionType.Root, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.HighFirst, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighSecond, duration, true); break; case ProgressionType3.Five2ndOneRootTwoRoot: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowSecond, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.Root, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.TwoMinor, InversionType.Root, duration, true); break; case ProgressionType3.One1stFive2ndFourRoot: samples1 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.OneMajor, InversionType.HighFirst, duration, true); samples2 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FiveMajor, InversionType.LowSecond, duration, true); samples3 = Inversion.CreateTriadInversionEx(doNoteNumber, TriadType.FourMajor, InversionType.Root, duration, true); break; default: throw new NotSupportedException($"ProgressionType {progressionType} is not supported."); } MixingSampleProvider msp1 = new MixingSampleProvider(samples1[0].WaveFormat); msp1.AddMixerInput(samples1[0]); msp1.AddMixerInput(samples1[1]); msp1.AddMixerInput(samples1[2]); if (samples1[3] != null) // Bass note. { msp1.AddMixerInput(samples1[3]); } MixingSampleProvider msp2 = new MixingSampleProvider(samples2[0].WaveFormat); msp2.AddMixerInput(samples2[0]); msp2.AddMixerInput(samples2[1]); msp2.AddMixerInput(samples2[2]); if (samples2[3] != null) // Bass note. { msp2.AddMixerInput(samples2[3]); } MixingSampleProvider msp3 = new MixingSampleProvider(samples3[0].WaveFormat); msp3.AddMixerInput(samples3[0]); msp3.AddMixerInput(samples3[1]); msp3.AddMixerInput(samples3[2]); if (samples3[3] != null) // Bass note. { msp3.AddMixerInput(samples3[3]); } var phrase = msp1 .FollowedBy(msp2) .FollowedBy(msp3); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
/// <summary> /// 截取音频后,对每个小音频获语音转文字,一个音频对应一段文字 /// </summary> /// <param name="startMilliSecond"></param> /// <param name="endMilliSecond"></param> /// <param name="reader"></param> /// <param name="sound_path"></param> /// <param name="_name"></param> /// <param name="language"></param> /// <param name="splitTime"></param> private void GetBaiduSpeech(TimeSpan startMilliSecond, TimeSpan endMilliSecond, string sound_path, int i, string language, double splitTime) { string newFile = ""; string _newFile = ""; string pcm_newFile = ""; bool need_delete = true; //将文件保存到新的文件夹(sound_path是原音频路径,newFolder是新的小音频路径,使用完成后将上传到服务器成功的音频删除) string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/"; newFolder = newFolder.Replace("/", "\\"); AudioFileReader reader = new AudioFileReader(sound_path); AudioFileReader _reader = null;//超过60s音频截取小音频 try { #region 为截取音频做准备 //开始时间往前取startMilliSecond一半的偏移,结束时间往后取间隔时间的一半的偏移 if (i == 0) { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(startMilliSecond.TotalMilliseconds / 2); } else { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(splitTime / 2); } if (endMilliSecond < reader.TotalTime)//最后一次不用取偏移 { endMilliSecond = endMilliSecond + TimeSpan.FromMilliseconds(splitTime / 2); } TimeSpan span = endMilliSecond - startMilliSecond; if (span.TotalSeconds > 60)//超过60s,只取50秒 { span = TimeSpan.FromSeconds(50); } var trimed = reader.Skip(startMilliSecond).Take(endMilliSecond - startMilliSecond); #endregion string fileName = Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 //重新存储到一个新的文件目录 if (!System.IO.Directory.Exists(newFolder)) { System.IO.Directory.CreateDirectory(newFolder); } //拼接后的文件路径 newFile = newFolder + fileName; //截取小音频文件 WaveFileWriter.CreateWaveFile16(newFile, trimed); //上传到文件服务器 string server_path = UploadFile.PostFile(newFile); if (Util.isNotNull(server_path)) { //上传成功 voiceFiles.Add(server_path); } else { need_delete = false; //上传失败,在服务器上的路径 voiceFiles.Add(Util.getServerPath() + "/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName); } //大于60s的需要再处理 if (span == TimeSpan.FromSeconds(50))//音频大于60s,只截取50s { //小音频 _reader = new AudioFileReader(sound_path); var _trimed = _reader.Skip(startMilliSecond).Take(span); //保存新的音频文件 string _fileName = "_" + Path.GetFileNameWithoutExtension(sound_path) + "_" + i + ".pcm";//重命名文件 _newFile = newFolder + _fileName; WaveFileWriter.CreateWaveFile16(_newFile, _trimed); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(_newFile, language, _trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } else { //将wav转换成pcm可以提高识别速度 pcm_newFile = NAudioHelper.GetPcmPath(newFile); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } } catch (Exception ex) { LogHelper.Error("GetBaiduSpeech:" + ex.Message); } finally { reader.Close(); reader.Dispose(); if (_reader != null) { _reader.Close(); _reader.Dispose(); } //删除文件 //if (File.Exists(_newFile)) //{ // File.Delete(_newFile); //} //if (need_delete) //{ // if (File.Exists(newFile)) // { // File.Delete(newFile); // } //} ////删除目录 //if (Directory.Exists(newFolder) && Directory.GetFiles(newFolder).Length == 0) //{ // Directory.Delete(newFolder); //} //if (File.Exists(pcm_newFile)) //{ // File.Delete(pcm_newFile); // string pcm_folder = Path.GetDirectoryName(pcm_newFile); // //删除目录 // if (Directory.Exists(pcm_folder) && Directory.GetFiles(pcm_folder).Length == 0) // { // Directory.Delete(pcm_folder); // } //} } }
/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5) { //通过NAudio读取音频文件流 AudioFileReader inputStream = null; string sound_path_mp3 = ""; try { if (sound_path.ToLower().EndsWith(".mp3")) { sound_path_mp3 = sound_path; //通过NAudio将文件转换为WAV格式,返回新的文件路径 sound_path = NAudioHelper.GetWavPath(sound_path); } inputStream = new AudioFileReader(sound_path); //WAV文件读取 WAVReader reader = new WAVReader(); reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime); //voicePoint是为了截取小音频 //voicePoint是双数的原因是有开始时间就有一个结束时间 //最后一次的时间要加上(结束时,没有停顿时间) if (voicePoint.Count % 2 != 0) { voicePoint.Add(inputStream.TotalTime); } LogHelper.Info("时间戳获取:成功"); //string totalTime=FfmpegHelper.getMediaDuration(sound_path); int _name = 1;//命名 //一个时间点对应一段音频对应一段文字 LogHelper.Info("语音转文字:开始"); for (int i = 0; i < voicePoint.Count; i += 2) { GetBaiduSpeech(voicePoint[i], voicePoint[i + 1], sound_path, _name, language, splitTime); ++_name; } //word原文 if (Path.GetExtension(word_path).Contains("doc") || Path.GetExtension(word_path).Contains("docx")) { originalText = NPOIHelper.ExcuteWordText(word_path); //word原文的集合 results = GetTextContrast(voiceFiles.ToArray(), voiceMilliSecond.ToArray(), baidu_text.ToArray(), originalText, language); } else if (Path.GetExtension(word_path).Contains("txt") || Path.GetExtension(word_path).Contains("lrc")) { string _originalText = Util.ReadTxt(word_path); originalText = string.Join("", _originalText.Split('|')); //word原文的集合 results = GetTextContrast(voiceFiles.ToArray(), voiceMilliSecond.ToArray(), baidu_text.ToArray(), _originalText, language); } } catch (Exception ex) { LogHelper.Error(ex.Message); } finally { inputStream.Close(); inputStream.Dispose(); isFinish = true; ////删除MP3文件 //if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) //{ // File.Delete(sound_path_mp3); // string mp3_folder = Path.GetDirectoryName(sound_path_mp3); // if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) // { // Directory.Delete(mp3_folder); // } //} ////删除word文件 //if (File.Exists(word_path)) //{ // File.Delete(word_path); // //删除word文件目录 // string word_folder = Path.GetDirectoryName(word_path); // if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) // { // Directory.Delete(word_folder); // } //} ////删除文件夹,删除原音频文件 //if (File.Exists(sound_path)) //{ // File.Delete(sound_path); // //删除原音频目录 // string sound_folder = Path.GetDirectoryName(sound_path); // if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) // { // Directory.Delete(sound_folder); // } //} } }
public ActionResult GetTriad(string doNoteName, int triadtype) { var triadType = (L2C5TriadType)triadtype; InversionType inversionType = GetRandomInversion(); TimeSpan noteDuration = TimeSpan.FromSeconds(3); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider[] samples; int newDoNoteNumber; // Used for other chords besides the I. switch (triadType) { case L2C5TriadType.OneMajor: samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.TwoMinor: newDoNoteNumber = doNoteNumber + Interval.UpMajor2nd; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMinor3rd, newDoNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.ThreeMinor: newDoNoteNumber = doNoteNumber + Interval.UpMajor3rd; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMinor3rd, newDoNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.FourMajor: newDoNoteNumber = doNoteNumber + Interval.UpPerfect4th; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMajor3rd, newDoNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.FiveMajor: newDoNoteNumber = doNoteNumber + Interval.UpPerfect5th; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMajor3rd, newDoNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.SixMinor: newDoNoteNumber = doNoteNumber + Interval.UpMajor6th; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMinor3rd, newDoNoteNumber + Interval.UpPerfect5th); break; case L2C5TriadType.SevenDiminished: newDoNoteNumber = doNoteNumber + Interval.UpMajor7th; samples = Inversion.CreateTriadInversionEx(inversionType, noteDuration, newDoNoteNumber, newDoNoteNumber + Interval.UpMinor3rd, newDoNoteNumber + Interval.UpDiminished5th); break; default: throw new NotSupportedException($"L2C5TriadType {triadType} is not supported."); } MixingSampleProvider msp = new MixingSampleProvider(samples[0].WaveFormat); msp.AddMixerInput(samples[0]); msp.AddMixerInput(samples[1]); msp.AddMixerInput(samples[2]); IWaveProvider wp = msp.ToWaveProvider(); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, wp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpanLrc(string sound_path, string word_path, string language, double splitTime = 1.5) { string sound_path_mp3 = ""; //音频格式转换后的文件 List <string> cut_sound_files = new List <string> (); //切分后的音频文件 string folder = Path.GetDirectoryName(sound_path); try { if (sound_path.ToLower().EndsWith(".mp3")) { sound_path_mp3 = sound_path; //通过NAudio将文件转换为WAV格式,返回新的文件路径 sound_path = NAudioHelper.GetWavPath(sound_path); } //音频处理,通过lrc的时间戳来截取音频与内容 //1.先从lrc里面获取时间戳与内容 if (Path.GetExtension(word_path).Contains("lrc")) { Dictionary <TimeSpan, string> lrcs = Util.ReadLrc(word_path); List <TimeSpan> times = lrcs.Keys.ToList(); for (int i = 0; i < times.Count; i++) { TextContrastResult _result = new TextContrastResult(); //2.通过时间戳,截取音频 string cut_sound_file = folder + "\\" + i + "_" + sys.getRandomStr() + Path.GetExtension(sound_path); TimeSpan _start = new TimeSpan(); TimeSpan _end = new TimeSpan(); if (i == times.Count - 1) { _end = TimeSpan.Parse(FfmpegHelper.getMediaDuration(sound_path)); } else { _end = times[i + 1]; } _start = times[i]; _result.timespan = times[i].TotalSeconds; _result.contractText = lrcs[times[i]]; _result.baiduText = lrcs[times[i]]; _result.precent = "100%"; bool is_success = FfmpegHelper.CutAudioFile(sound_path, cut_sound_file, _start, _end - _start); if (is_success) { cut_sound_files.Add(cut_sound_file); } //上传到服务器 if (File.Exists(cut_sound_file)) { string server_path = UploadFile.PostFile(cut_sound_file); _result.file_url = server_path; } results.Add(_result); } } //原文 string _originalText = Util.ReadTxt(word_path); originalText = string.Join("", _originalText.Split('|')); } catch (Exception ex) { LogHelper.Error(ex.Message); } finally { isFinish = true; //删除切分的文件 foreach (string cut_sound_file in cut_sound_files) { if (Util.isNotNull(cut_sound_file) && File.Exists(cut_sound_file)) { File.Delete(cut_sound_file); } } //删除MP3文件 if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) { File.Delete(sound_path_mp3); string mp3_folder = Path.GetDirectoryName(sound_path_mp3); if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) { Directory.Delete(mp3_folder); } } //删除word文件 if (File.Exists(word_path)) { File.Delete(word_path); //删除word文件目录 string word_folder = Path.GetDirectoryName(word_path); if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) { Directory.Delete(word_folder); } } //删除文件夹,删除原音频文件 if (File.Exists(sound_path)) { File.Delete(sound_path); //删除原音频目录 string sound_folder = Path.GetDirectoryName(sound_path); if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) { Directory.Delete(sound_folder); } } } }
public ActionResult Get2ChordProgression(string doNoteName, int progressiontype) { var progressionType = (L2C4ProgressionType)progressiontype; TimeSpan noteDuration = TimeSpan.FromSeconds(2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider[] samples1; ISampleProvider[] samples2; switch (progressionType) { case L2C4ProgressionType.Five2ndToOne1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th); samples2 = Inversion.CreateTriadInversionEx(InversionType.HighFirst, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.Five1stToOneRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.FiveRootToOne2nd: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th); samples2 = Inversion.CreateTriadInversionEx(InversionType.HighSecond, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.Four1stToOne2nd: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.FourRootToOne1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave); samples2 = Inversion.CreateTriadInversionEx(InversionType.HighFirst, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.Four2ndToOneRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); break; case L2C4ProgressionType.OneRootToFlatTwoRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpMinor2nd, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpAugmented5th); break; case L2C4ProgressionType.OneRootToFlatTwo2nd: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpMinor2nd, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpAugmented5th); break; case L2C4ProgressionType.OneRootToSevenRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber + Interval.DownMinor2nd, doNoteNumber + Interval.UpMinor3rd, doNoteNumber + Interval.UpDiminished5th); break; case L2C4ProgressionType.OneRootToSeven1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th); samples2 = Inversion.CreateTriadInversionEx(InversionType.HighFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.DownMinor2nd, doNoteNumber + Interval.UpMinor3rd, doNoteNumber + Interval.UpDiminished5th); break; default: throw new NotSupportedException($"ProgressionType {progressionType} is not supported."); } MixingSampleProvider msp1 = new MixingSampleProvider(samples1[0].WaveFormat); msp1.AddMixerInput(samples1[0]); msp1.AddMixerInput(samples1[1]); msp1.AddMixerInput(samples1[2]); MixingSampleProvider msp2 = new MixingSampleProvider(samples2[0].WaveFormat); msp2.AddMixerInput(samples2[0]); msp2.AddMixerInput(samples2[1]); msp2.AddMixerInput(samples2[2]); var phrase = msp1 .FollowedBy(msp2); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
private MemoryStream GetMelodicInterval(string doNoteName, L1C5MelodicDrillType drillType) { double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; TimeSpan halfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2; switch (drillType) { #region 4ths. case L1C5MelodicDrillType.DoFa4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); break; case L1C5MelodicDrillType.ReSo4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); break; case L1C5MelodicDrillType.MiLa4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); break; case L1C5MelodicDrillType.FaTi4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; case L1C5MelodicDrillType.SoDo4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); break; case L1C5MelodicDrillType.LaRe4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, halfNoteDuration); break; case L1C5MelodicDrillType.TiMi4Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); break; case L1C5MelodicDrillType.FaDo4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); break; case L1C5MelodicDrillType.SoRe4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, halfNoteDuration); break; case L1C5MelodicDrillType.LaMi4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); break; case L1C5MelodicDrillType.TiFa4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); break; case L1C5MelodicDrillType.DoSo4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); break; case L1C5MelodicDrillType.ReLa4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); break; case L1C5MelodicDrillType.MiTi4Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; #endregion 4ths. #region 5ths. case L1C5MelodicDrillType.DoSo5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); break; case L1C5MelodicDrillType.ReLa5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); break; case L1C5MelodicDrillType.MiTi5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; case L1C5MelodicDrillType.FaDo5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); break; case L1C5MelodicDrillType.SoRe5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, halfNoteDuration); break; case L1C5MelodicDrillType.LaMi5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); break; case L1C5MelodicDrillType.TiFa5Asc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect11th, halfNoteDuration); break; case L1C5MelodicDrillType.SoDo5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, halfNoteDuration); break; case L1C5MelodicDrillType.LaRe5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, halfNoteDuration); break; case L1C5MelodicDrillType.TiMi5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, halfNoteDuration); break; case L1C5MelodicDrillType.DoFa5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, halfNoteDuration); break; case L1C5MelodicDrillType.ReSo5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, halfNoteDuration); break; case L1C5MelodicDrillType.MiLa5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, halfNoteDuration); break; case L1C5MelodicDrillType.FaTi5Desc: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect11th, halfNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, halfNoteDuration); break; #endregion 5ths. default: throw new NotSupportedException($"L1C5MelodicDrillType '{drillType}' is not supported."); } var phrase = note1 .FollowedBy(note2); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
private MemoryStream GetHarmonicDrillEx(string doNoteName, L1C5HarmonicDrillType drillType) { TimeSpan duration = TimeSpan.FromSeconds(3); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2; switch (drillType) { #region 4ths. case L1C5HarmonicDrillType.DoFa4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, duration); break; case L1C5HarmonicDrillType.ReSo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, duration); break; case L1C5HarmonicDrillType.MiLa4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, duration); break; case L1C5HarmonicDrillType.FaTi4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); break; case L1C5HarmonicDrillType.SoDo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, duration); break; case L1C5HarmonicDrillType.LaRe4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, duration); break; case L1C5HarmonicDrillType.TiMi4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, duration); break; #endregion 4ths. #region 5ths. case L1C5HarmonicDrillType.DoSo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, duration); break; case L1C5HarmonicDrillType.ReLa5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, duration); break; case L1C5HarmonicDrillType.MiTi5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); break; case L1C5HarmonicDrillType.FaDo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, duration); break; case L1C5HarmonicDrillType.SoRe5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, duration); break; case L1C5HarmonicDrillType.LaMi5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, duration); break; case L1C5HarmonicDrillType.TiFa5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, duration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect11th, duration); break; #endregion 5ths. default: throw new NotSupportedException($"L1C5HarmonicDrillType '{drillType}' is not supported."); } MixingSampleProvider msp = new MixingSampleProvider(note1.WaveFormat); msp.AddMixerInput(note1); msp.AddMixerInput(note2); var stwp = new SampleToWaveProvider(msp); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5) { //通过NAudio读取音频文件流 AudioFileReader inputStream = null; string sound_path_mp3 = ""; try { if (sound_path.ToLower().EndsWith(".mp3")) { sound_path_mp3 = sound_path; //通过NAudio将文件转换为WAV格式,返回新的文件路径 sound_path = NAudioHelper.GetWavPath(sound_path); } inputStream = new AudioFileReader(sound_path); //WAV文件读取 WAVReader reader = new WAVReader(); //reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime); reader.GetTimeSpan(sound_path, out voicePoint, out musicContents, splitTime); //voicePoint是为了截取小音频 //voicePoint是双数的原因是有开始时间就有一个结束时间 //最后一次的时间要加上(结束时,没有停顿时间) if (voicePoint.Count % 2 != 0) { voicePoint.Add(inputStream.TotalTime); } LogHelper.Info("时间戳获取:成功"); //string totalTime=FfmpegHelper.getMediaDuration(sound_path); int _name = 1;//命名 //一个时间点对应一段音频对应一段文字 LogHelper.Info("语音转文字:开始"); for (int i = 0; i < voicePoint.Count; i += 2) { var para = new BaiduRead() { voice_time = musicContents.Select(o => o.voiceMilliSecond).ToList()[i / 2], start = voicePoint[i], end = voicePoint[i + 1], sound_path = sound_path, file_name = _name, language = language, splitTime = splitTime }; Task task_max = Task.Factory.StartNew(() => { GetBaiduSpeech(para); }); Task.WaitAll(task_max); //GetBaiduSpeech(para); ++_name; } //word原文 originalText = NPOIHelper.ExcuteWordText(word_path); //word原文的集合 results = GetTextContrast(musicContents, originalText, language); } catch (Exception ex) { LogHelper.Error(ex.Message); } #region 除文件 finally { inputStream.Close(); inputStream.Dispose(); isFinish = true; //删除MP3文件 if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) { File.Delete(sound_path_mp3); string mp3_folder = Path.GetDirectoryName(sound_path_mp3); if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) { Directory.Delete(mp3_folder); } } //删除word文件 if (File.Exists(word_path)) { File.Delete(word_path); //删除word文件目录 string word_folder = Path.GetDirectoryName(word_path); if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) { Directory.Delete(word_folder); } } //删除文件夹,删除原音频文件 if (File.Exists(sound_path)) { File.Delete(sound_path); //删除原音频目录 string sound_folder = Path.GetDirectoryName(sound_path); if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) { Directory.Delete(sound_folder); } } } #endregion }
public ActionResult AudioAndDictation(int resolutionType, string keySignature, double bpm, int numberOfMeasures) { // We'll add stuff to the Dictionary and return as JSON. var dict = new Dictionary <string, string>(); #region Audio //double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; TimeSpan quarterNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis); TimeSpan halfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 2); TimeSpan dottedHalfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 3); TimeSpan wholeNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 4); // Setup the scale note numbers. int[] scaleNoteNumbers = new int[] { 38, 39, 41, 43, 44, 46, 48, 50, 51 }; // C Major, with a low TI. scaleNoteNumbers = NoteHelper.TransposeScaleNoteNumbers(scaleNoteNumbers, keySignature); // The initial DO. ISampleProvider wholeDoNote = NAudioHelper.GetSampleProvider(scaleNoteNumbers[1], wholeNoteDuration); // Four metronome ticks before the transcription part plays. ISampleProvider[] ticks = new ISampleProvider[4]; string tickFile = HostingEnvironment.MapPath($"~/Samples/Woodblock.wav"); for (int i = 0; i < ticks.Length; i++) { ticks[i] = NAudioHelper.GetSampleProviderFromFile(tickFile, quarterNoteDuration); } List <string> measureRhythms = GetNoteRhythms(); int randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); string measureRhythm1 = measureRhythms[randomInt]; randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); string measureRhythm2 = measureRhythms[randomInt]; if (numberOfMeasures == 2) { while ((measureRhythm1.Split(',').Count() + measureRhythm2.Split(',').Count()) % 2 == 1) // Ensure an even number of notes, so there's always a (reverse) resolution. { randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm2 = measureRhythms[randomInt]; } } randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); string measureRhythm3 = measureRhythms[randomInt]; randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); string measureRhythm4 = measureRhythms[randomInt]; if (numberOfMeasures == 4) { while ((measureRhythm1.Split(',').Count() + measureRhythm2.Split(',').Count() + measureRhythm3.Split(',').Count() + measureRhythm4.Split(',').Count()) % 2 == 1) { randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm4 = measureRhythms[randomInt]; } } string[] measureRhythmSplit1 = measureRhythm1.Split(','); int numberOfNotes1 = measureRhythmSplit1.Length; string[] measureRhythmSplit2 = measureRhythm2.Split(','); int numberOfNotes2 = measureRhythmSplit2.Length; string[] measureRhythmSplit3 = measureRhythm3.Split(','); int numberOfNotes3 = measureRhythmSplit3.Length; string[] measureRhythmSplit4 = measureRhythm4.Split(','); int numberOfNotes4 = measureRhythmSplit4.Length; ISampleProvider[] notes1 = new ISampleProvider[numberOfNotes1]; ISampleProvider[] notes2 = new ISampleProvider[numberOfNotes2]; ISampleProvider[] notes3 = new ISampleProvider[numberOfNotes3]; ISampleProvider[] notes4 = new ISampleProvider[numberOfNotes4]; Queue <int> noteNumberQueue; switch (resolutionType) { case 1: noteNumberQueue = GetResolutionIntQueue(scaleNoteNumbers, 16, 1); // 16 notes max. break; case 2: noteNumberQueue = GetResolutionIntQueue(scaleNoteNumbers, 16, 2); break; case 3: noteNumberQueue = GetResolutionIntQueue(scaleNoteNumbers, 16, 3); break; default: throw new NotSupportedException($"ResolutionType '{resolutionType}' is not supported."); } int[] measureNoteNumbers1 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes1, noteNumberQueue); int[] measureNoteNumbers2 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes2, noteNumberQueue); int[] measureNoteNumbers3 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes3, noteNumberQueue); int[] measureNoteNumbers4 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes4, noteNumberQueue); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(new TimeSpan(), quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit1, notes1, measureNoteNumbers1); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(new TimeSpan(), quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit2, notes2, measureNoteNumbers2); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(new TimeSpan(), quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit3, notes3, measureNoteNumbers3); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(new TimeSpan(), quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit4, notes4, measureNoteNumbers4); ISampleProvider phrase = wholeDoNote; foreach (var tick in ticks) { phrase = phrase.FollowedBy(tick); } foreach (var note1 in notes1) { phrase = phrase.FollowedBy(note1); } foreach (var note2 in notes2) { phrase = phrase.FollowedBy(note2); } if (numberOfMeasures == 4) { foreach (var note3 in notes3) { phrase = phrase.FollowedBy(note3); } foreach (var note4 in notes4) { phrase = phrase.FollowedBy(note4); } } // HACK: Use an empty note because without it, the audio gets cut short. ISampleProvider emptyNote = NAudioHelper.GetSampleProvider(0, 0, SignalGeneratorType.White, halfNoteDuration); phrase = phrase.FollowedBy(emptyNote); SampleToWaveProvider stwp = new SampleToWaveProvider(phrase); MixingSampleProvider msp = new MixingSampleProvider(stwp.WaveFormat); msp.AddMixerInput(stwp); int totalTicks = 8 + (4 * numberOfMeasures); ISampleProvider[] metronomeTicks = new ISampleProvider[totalTicks]; // The first two measures have zero gain, as this is the initial DO whole note and metronome ticks. for (int i = 0; i < 8; i++) { metronomeTicks[i] = NAudioHelper.GetSampleProvider(0, 0, SignalGeneratorType.White, quarterNoteDuration); } for (int i = 8; i < totalTicks; i++) { metronomeTicks[i] = NAudioHelper.GetSampleProviderFromFile(tickFile, quarterNoteDuration); } ISampleProvider metronomePhrase = metronomeTicks[0]; for (int i = 0; i < metronomeTicks.Length; i++) { metronomePhrase = metronomePhrase.FollowedBy(metronomeTicks[i]); } msp.AddMixerInput(metronomePhrase); IWaveProvider wp = msp.ToWaveProvider(); MemoryStream wavStream = new MemoryStream(); //WaveFileWriter.WriteWavFileToStream(wavStream, stwp); WaveFileWriter.WriteWavFileToStream(wavStream, wp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); dict.Add("src", fileName); #endregion Audio #region Notation string[] noteNames1 = new string[numberOfNotes1]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers1, noteNames1); string[] noteNames2 = new string[numberOfNotes2]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers2, noteNames2); string[] noteNames3 = new string[numberOfNotes3]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers3, noteNames3); string[] noteNames4 = new string[numberOfNotes4]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers4, noteNames4); string script1 = NoteHelper.GetEasyScoreScript("transcription1", noteNames1, measureRhythmSplit1, keySignature, true); dict.Add("theScript1", script1); string script2 = NoteHelper.GetEasyScoreScript("transcription2", noteNames2, measureRhythmSplit2, keySignature, false); dict.Add("theScript2", script2); if (numberOfMeasures == 4) { string script3 = NoteHelper.GetEasyScoreScript("transcription3", noteNames3, measureRhythmSplit3, keySignature, false); dict.Add("theScript3", script3); string script4 = NoteHelper.GetEasyScoreScript("transcription4", noteNames4, measureRhythmSplit4, keySignature, false); dict.Add("theScript4", script4); } #endregion Notation var json = Json(dict, JsonRequestBehavior.AllowGet); return(json); }
public ActionResult Get3ChordProgressionEx(string doNoteName, int progressiontype) { var progressionType = (ProgressionType)progressiontype; TimeSpan noteDuration = TimeSpan.FromSeconds(2); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider[] samples1; ISampleProvider[] samples2; ISampleProvider[] samples3; switch (progressionType) { case ProgressionType.Four2ndFive1stOneRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); break; case ProgressionType.OneRootFour2ndFive1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); break; case ProgressionType.Four2ndOneRootFive1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); break; case ProgressionType.OneRootFive1stSixMin1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); break; case ProgressionType.SixMin2ndFourRootOne1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.HighFirst, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); break; case ProgressionType.OneRootSixMin1stFive1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); break; case ProgressionType.FiveRootFourRootSixMin2nd: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); break; case ProgressionType.FourRootFiveRootSixMinRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); break; case ProgressionType.SixMin1stOne2ndFour1st: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.LowFirst, noteDuration.Add(noteDuration), doNoteNumber + Interval.UpPerfect4th, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, (doNoteNumber + Interval.UpPerfect4th).BassNoteNumber()); break; case ProgressionType.Five2ndSixMin2ndOneRoot: samples1 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpPerfect5th, doNoteNumber + Interval.UpMajor7th, doNoteNumber + Interval.UpMajor9th, (doNoteNumber + Interval.UpPerfect5th).BassNoteNumber()); samples2 = Inversion.CreateTriadInversionEx(InversionType.LowSecond, noteDuration, doNoteNumber + Interval.UpMajor6th, doNoteNumber + Interval.UpPerfectOctave, doNoteNumber + Interval.UpMajor10th, (doNoteNumber + Interval.UpMajor6th).BassNoteNumber()); samples3 = Inversion.CreateTriadInversionEx(InversionType.Root, noteDuration.Add(noteDuration), doNoteNumber, doNoteNumber + Interval.UpMajor3rd, doNoteNumber + Interval.UpPerfect5th, doNoteNumber.BassNoteNumber()); break; default: throw new NotSupportedException($"ProgressionType {progressionType} is not supported."); } // First chord. MixingSampleProvider msp1 = new MixingSampleProvider(samples1[0].WaveFormat); msp1.AddMixerInput(samples1[0]); msp1.AddMixerInput(samples1[1]); msp1.AddMixerInput(samples1[2]); if (samples1[3] != null) // Bass note. { msp1.AddMixerInput(samples1[3]); } // Second chord. MixingSampleProvider msp2 = new MixingSampleProvider(samples2[0].WaveFormat); msp2.AddMixerInput(samples2[0]); msp2.AddMixerInput(samples2[1]); msp2.AddMixerInput(samples2[2]); if (samples2[3] != null) // Bass note. { msp2.AddMixerInput(samples2[3]); } // Third chord. MixingSampleProvider msp3 = new MixingSampleProvider(samples3[0].WaveFormat); msp3.AddMixerInput(samples3[0]); msp3.AddMixerInput(samples3[1]); msp3.AddMixerInput(samples3[2]); if (samples3[3] != null) // Bass note. { msp3.AddMixerInput(samples3[3]); } var phrase = msp1 .FollowedBy(msp2) .FollowedBy(msp3); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); return(Redirect($"~/Temp/{fileName}")); }
private void InitRequest() { //Fill the viewbag values ViewBag.Volume = NAudioHelper.GetCurrentSoundLevel(); ViewBag.AudioDeviceExists = NAudioHelper.GetDefaultAudioEndpoint() != null; }
private MemoryStream GetMelodicDrillEx(string doNoteName, L1C5MelodicDrillType drillType) { double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; double eighthNoteMillis = quarterNoteMillis / 2; TimeSpan eighthNoteDuration = TimeSpan.FromMilliseconds(eighthNoteMillis); TimeSpan quarterNoteDuration = eighthNoteDuration.Add(eighthNoteDuration); TimeSpan wholeNoteDuration = quarterNoteDuration.Add(quarterNoteDuration).Add(quarterNoteDuration).Add(quarterNoteDuration); string doFileName = NAudioHelper.GetFileNameFromNoteName(doNoteName); doFileName = Path.GetFileName(doFileName); int doNoteNumber = int.Parse(doFileName.Split('.')[0]); ISampleProvider note1, note2, note3, note4, note5, note6; switch (drillType) { #region 4ths. case L1C5MelodicDrillType.DoDoDoFaFaMi4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, wholeNoteDuration); break; case L1C5MelodicDrillType.ReReSoSoSoSo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, wholeNoteDuration); break; case L1C5MelodicDrillType.MiMiMiLaLaSo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, wholeNoteDuration); break; case L1C5MelodicDrillType.FaFaTiTiTiDo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.SoSoSoDoDoDo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.LaLaReReReDo4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.TiTiTiMiMiMi4: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, wholeNoteDuration); break; #endregion 4ths. #region 5ths. case L1C5MelodicDrillType.DoDoDoSoSoSo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, wholeNoteDuration); break; case L1C5MelodicDrillType.ReReLaLaLaSo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor2nd, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, wholeNoteDuration); break; case L1C5MelodicDrillType.MiMiMiTiTiDo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor3rd, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor7th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.FaFaDoDoDoDo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.SoSoSoReReDo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect5th, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor9th, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfectOctave, wholeNoteDuration); break; case L1C5MelodicDrillType.LaLaMiMiMiMi5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor6th, quarterNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, quarterNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, eighthNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, eighthNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpMajor10th, wholeNoteDuration); break; case L1C5MelodicDrillType.FaFaFaTiTiDo5: note1 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, quarterNoteDuration); note2 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, eighthNoteDuration); note3 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.UpPerfect4th, eighthNoteDuration); note4 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.DownMinor2nd, quarterNoteDuration); note5 = NAudioHelper.GetSampleProvider(doNoteNumber + Interval.DownMinor2nd, quarterNoteDuration); note6 = NAudioHelper.GetSampleProvider(doNoteNumber, wholeNoteDuration); break; #endregion 5ths. default: throw new NotSupportedException($"L1C5MelodicDrillType '{drillType}' is not supported."); } var phrase = note1 .FollowedBy(note2) .FollowedBy(note3) .FollowedBy(note4) .FollowedBy(note5) .FollowedBy(note6); var stwp = new SampleToWaveProvider(phrase); MemoryStream wavStream = new MemoryStream(); WaveFileWriter.WriteWavFileToStream(wavStream, stwp); wavStream.Position = 0; return(wavStream); }
public ActionResult AudioAndDictation(int intervalType, string keySignature, double bpm, int numberOfMeasures, string smallestRhythmicUnit, bool includeC2) { _log.Debug($"intervalType: {intervalType}, keySignature: {keySignature}, bpm: {bpm}, numberOfMeasures: {numberOfMeasures}, smallestRhythmicUnit: {smallestRhythmicUnit}, includeC2: {includeC2}"); L1C3IntervalType intType = (L1C3IntervalType)intervalType; bool includeEighthNoteRhythms = smallestRhythmicUnit.ToUpper() == "EIGHTH"; // We'll add stuff to the Dictionary and return as JSON. var dict = new Dictionary <string, string>(); #region Audio //double bpm = double.Parse(ConfigurationManager.AppSettings["BPM"]); double quarterNoteMillis = (60 / bpm) * 1000; TimeSpan quarterNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis); TimeSpan halfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 2); TimeSpan dottedHalfNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 3); TimeSpan wholeNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis * 4); TimeSpan eighthNoteDuration = TimeSpan.FromMilliseconds(quarterNoteMillis / 2); // Setup the scale note numbers. int[] scaleNoteNumbers = new int[] { 38, 39, 41, 43, 44, 46, 48, 50, 51, 53, 55, 56, 58, 60 }; // C Major, low TI to high SO. scaleNoteNumbers = NoteHelper.TransposeScaleNoteNumbers(scaleNoteNumbers, keySignature); // The initial DO. ISampleProvider wholeDoNote = NAudioHelper.GetSampleProvider(scaleNoteNumbers[1], wholeNoteDuration); // Four metronome ticks before the transcription part plays. ISampleProvider[] ticks = new ISampleProvider[4]; string tickFile = HostingEnvironment.MapPath($"~/Samples/Woodblock.wav"); for (int i = 0; i < ticks.Length; i++) { ticks[i] = NAudioHelper.GetSampleProviderFromFile(tickFile, quarterNoteDuration); } List <string> measureRhythms = GetNoteRhythms(includeEighthNoteRhythms); int randomInt; string measureRhythm1; string measureRhythm2; // Ensure there's at least 4 notes per 2 measures, as we need at least one C1 resolutiona and at least 1 C2 interval. // Ensure an even number of notes, so the interval is complete. // Ensure there's exactly one pair of eighth notes for each two measure phrase. while (true) { randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm1 = measureRhythms[randomInt]; randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm2 = measureRhythms[randomInt]; int totalNotes = measureRhythm1.Split(',').Count() + measureRhythm2.Split(',').Count(); int totalEighthNotes = measureRhythm1.Split(',').Where(w => w == "8").Count() + measureRhythm2.Split(',').Where(w => w == "8").Count(); // Ensure a minimum number of notes for the 2 measure phrase. This could be 4, or 6 if C2 intervals are included. int minimumNumberOfNotes = includeC2 ? 6 : 4; if (totalNotes < minimumNumberOfNotes) { continue; } // Ensure an even number of notes. if (totalNotes % 2 != 0) { continue; } // If quarter note is smallest rhythmic unit, ensure no eighth notes. if (!includeEighthNoteRhythms && totalEighthNotes > 0) { continue; } // If eighth note is smallest rhythic unit, ensure just one pair of eighth notes. if (includeEighthNoteRhythms && totalEighthNotes != 2) { continue; } break; } string measureRhythm3 = string.Empty; string measureRhythm4 = string.Empty; // TODO: Figure out how to refactor this. Kinda messy, but for now we need to populate measures 3 and 4 even if the user // choose just 2 measures. randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm3 = measureRhythms[randomInt]; randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm4 = measureRhythms[randomInt]; //if (numberOfMeasures == 4) if (true) { while (true) { randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm3 = measureRhythms[randomInt]; randomInt = NoteHelper.GetRandomInt(0, measureRhythms.Count); measureRhythm4 = measureRhythms[randomInt]; int totalNotes = measureRhythm3.Split(',').Count() + measureRhythm4.Split(',').Count(); int totalEighthNotes = measureRhythm3.Split(',').Where(w => w == "8").Count() + measureRhythm4.Split(',').Where(w => w == "8").Count(); // Ensure a minimum number of notes for the 2 measure phrase. This could be 4, or 6 if C2 intervals are included. int minimumNumberOfNotes = includeC2 ? 6 : 4; if (totalNotes < minimumNumberOfNotes) { continue; } // Ensure an even number of notes. if (totalNotes % 2 != 0) { continue; } // If quarter note is smallest rhythmic unit, ensure no eighth notes. if (!includeEighthNoteRhythms && totalEighthNotes > 0) { continue; } // If eighth note is smallest rhythic unit, ensure just one pair of eighth notes. if (includeEighthNoteRhythms && totalEighthNotes != 2) { continue; } break; } } string[] measureRhythmSplit1 = measureRhythm1.Split(','); int numberOfNotes1 = measureRhythmSplit1.Length; string[] measureRhythmSplit2 = measureRhythm2.Split(','); int numberOfNotes2 = measureRhythmSplit2.Length; string[] measureRhythmSplit3 = measureRhythm3.Split(','); int numberOfNotes3 = measureRhythmSplit3.Length; string[] measureRhythmSplit4 = measureRhythm4.Split(','); int numberOfNotes4 = measureRhythmSplit4.Length; ISampleProvider[] notes1 = new ISampleProvider[numberOfNotes1]; ISampleProvider[] notes2 = new ISampleProvider[numberOfNotes2]; ISampleProvider[] notes3 = new ISampleProvider[numberOfNotes3]; ISampleProvider[] notes4 = new ISampleProvider[numberOfNotes4]; int first2MeasuresNumberOfNotes = numberOfNotes1 + numberOfNotes2; int second2MeasuresNumberOfNotes = numberOfNotes3 + numberOfNotes4; Queue <int> noteNumberQueue = new Queue <int>(); bool criteriaSatisfied = false; int numberOfTries = 0; int maxNumberOfTries = 1999; var parametersToString = string.Empty; try { while (!(noteNumberQueue.AllStepsWithinRange(12) && noteNumberQueue.AllNotesWithinRange(12) && criteriaSatisfied)) { numberOfTries++; //_log.Info($"numberOfTries: {numberOfTries}"); //_log.Info($"measureRhythm1: {measureRhythm1}"); //_log.Info($"measureRhythm2: {measureRhythm2}"); if (numberOfTries > maxNumberOfTries) { throw new PhraseGenerationException($"Aborted phrase generation after {numberOfTries} tries."); } noteNumberQueue = GetIntervalIntQueue(scaleNoteNumbers, first2MeasuresNumberOfNotes, second2MeasuresNumberOfNotes, intType, out criteriaSatisfied, includeC2, out parametersToString); } } catch (PhraseGenerationException pgEx) { _log.Trace($"{numberOfTries} - {parametersToString}"); _log.Error(pgEx, $"The maxNumberOfTries ({maxNumberOfTries}) has been exceeded."); dict.Add("hasError", "yep"); dict.Add("numberOfTries", numberOfTries.ToString()); var jsonEx = Json(dict, JsonRequestBehavior.AllowGet); return(jsonEx); } _log.Trace(numberOfTries); int[] measureNoteNumbers1 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes1, noteNumberQueue); int[] measureNoteNumbers2 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes2, noteNumberQueue); int[] measureNoteNumbers3 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes3, noteNumberQueue); int[] measureNoteNumbers4 = NoteHelper.PopulateNoteNumbersFromQueue(numberOfNotes4, noteNumberQueue); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(eighthNoteDuration, quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit1, notes1, measureNoteNumbers1); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(eighthNoteDuration, quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit2, notes2, measureNoteNumbers2); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(eighthNoteDuration, quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit3, notes3, measureNoteNumbers3); NoteHelper.CreateSamplesFromRhythmsAndNoteNames(eighthNoteDuration, quarterNoteDuration, halfNoteDuration, dottedHalfNoteDuration, wholeNoteDuration, measureRhythmSplit4, notes4, measureNoteNumbers4); ISampleProvider phrase = wholeDoNote; foreach (var tick in ticks) { phrase = phrase.FollowedBy(tick); } foreach (var note1 in notes1) { phrase = phrase.FollowedBy(note1); } foreach (var note2 in notes2) { phrase = phrase.FollowedBy(note2); } if (numberOfMeasures == 4) { foreach (var note3 in notes3) { phrase = phrase.FollowedBy(note3); } foreach (var note4 in notes4) { phrase = phrase.FollowedBy(note4); } } // HACK: Use an empty note because without it, the audio gets cut short. ISampleProvider emptyNote = NAudioHelper.GetSampleProvider(0, 0, SignalGeneratorType.White, halfNoteDuration); phrase = phrase.FollowedBy(emptyNote); SampleToWaveProvider stwp = new SampleToWaveProvider(phrase); MixingSampleProvider msp = new MixingSampleProvider(stwp.WaveFormat); msp.AddMixerInput(stwp); int totalTicks = 8 + (4 * numberOfMeasures); ISampleProvider[] metronomeTicks = new ISampleProvider[totalTicks]; // The first two measures have zero gain, as this is the initial DO whole note and metronome ticks. for (int i = 0; i < 8; i++) { metronomeTicks[i] = NAudioHelper.GetSampleProvider(0, 0, SignalGeneratorType.White, quarterNoteDuration); } for (int i = 8; i < totalTicks; i++) { metronomeTicks[i] = NAudioHelper.GetSampleProviderFromFile(tickFile, quarterNoteDuration); } ISampleProvider metronomePhrase = metronomeTicks[0]; for (int i = 0; i < metronomeTicks.Length; i++) { metronomePhrase = metronomePhrase.FollowedBy(metronomeTicks[i]); } msp.AddMixerInput(metronomePhrase); IWaveProvider wp = msp.ToWaveProvider(); MemoryStream wavStream = new MemoryStream(); //WaveFileWriter.WriteWavFileToStream(wavStream, stwp); WaveFileWriter.WriteWavFileToStream(wavStream, wp); wavStream.Position = 0; wavStream.WavToMp3File(out string fileName); dict.Add("src", fileName); dict.Add("numberOfTries", numberOfTries.ToString()); #endregion Audio #region Notation string[] noteNames1 = new string[numberOfNotes1]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers1, noteNames1); string[] noteNames2 = new string[numberOfNotes2]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers2, noteNames2); string[] noteNames3 = new string[numberOfNotes3]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers3, noteNames3); string[] noteNames4 = new string[numberOfNotes4]; NoteHelper.AdjustNoteNamesForKey(keySignature, measureNoteNumbers4, noteNames4); string script1 = NoteHelper.GetEasyScoreScript3("transcription1", noteNames1, measureRhythmSplit1, keySignature, true); //string script1 = NoteHelper.GetMusicXmlScript("transcription1", noteNames1, measureRhythmSplit1, keySignature, false); dict.Add("theScript1", script1); string script2 = NoteHelper.GetEasyScoreScript3("transcription2", noteNames2, measureRhythmSplit2, keySignature, false); dict.Add("theScript2", script2); if (numberOfMeasures == 4) { string script3 = NoteHelper.GetEasyScoreScript3("transcription3", noteNames3, measureRhythmSplit3, keySignature, false); dict.Add("theScript3", script3); string script4 = NoteHelper.GetEasyScoreScript3("transcription4", noteNames4, measureRhythmSplit4, keySignature, false); dict.Add("theScript4", script4); } #endregion Notation var json = Json(dict, JsonRequestBehavior.AllowGet); return(json); }