예제 #1
0
        public SuccessViewModel CalculateSuccess(SongViewModel song)
        {
            try
            {
                SongBE entity;
                entity = Mapper.Map <SongViewModel, SongBE>(song);
                SongDAL          songDAL          = new SongDAL();
                SuccessViewModel successViewModel = new SuccessViewModel();


                var listSongs = songDAL.GetSongsToCalculate(entity.Category);


                (int sampleRate, double[] audio) = WavFile.ReadMono(FileUtils.GetRepoMusicPath(song.SongKey));


                var spec = new Spectrogram.Spectrogram(sampleRate / 2, fftSize: (16384 / 8), stepSize: (2500 * 5), maxFreq: 2200);
                spec.Add(audio);
                var tempPath = Path.GetTempPath();
                spec.SaveImage(tempPath + "/" + song.SongKey + ".jpg", intensity: 5, dB: true);

                var file = FileUtils.GetImageBytes(tempPath + "/" + song.SongKey + ".jpg");
                successViewModel.ImageBase64 = "data:image/jpg;base64," + Convert.ToBase64String(file);

                var bmHash = this.GetHash(spec.GetBitmap());



                List <Spectrogram.Spectrogram> spectrograms = new List <Spectrogram.Spectrogram>();

                foreach (var son in listSongs)
                {
                    (int sampleRateSong, double[] audioSong) = WavFile.ReadMono(FileUtils.GetRepoMusicPath(son.SongKey));
                    var specSong = new Spectrogram.Spectrogram(sampleRateSong / 2, fftSize: (16384 / 8), stepSize: (2500 * 5), maxFreq: 2200);
                    specSong.Add(audioSong);
                    spectrograms.Add(specSong);
                }

                int equalElements = 0;

                foreach (var sp in spectrograms)
                {
                    equalElements += bmHash.Zip(this.GetHash(sp.GetBitmap()), (i, j) => i == j).Count(eq => eq);
                }

                var con = Convert.ToInt32(equalElements / spectrograms.Count);
                successViewModel.Percentage = Convert.ToInt32((con * 100) / bmHash.Count);
                return(successViewModel);
            }
            catch (Exception ex)
            {
                throw new Exception(Messages.Generic_Error);
            }
        }
예제 #2
0
        //What the program does every clock cycle
        private void SpecTimer_tick(object sender, EventArgs e)
        {
            double[] newAudio = listener.GetNewAudio();
            if (!specPaused)
            {
                spec.Add(newAudio, process: false);
            }

            double threshold = m_RealTimeFilterEnabled ? m_WhiteNoiseThreshold : 0;

            spec.Process(threshold);
            DisplaySpectrogram();
        }
예제 #3
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            double[] newAudio = listener.GetNewAudio();
            spec.Add(newAudio, process: false);

            double multiplier = Brightness / 20.0;

            if (spec.FftsToProcess > 0)
            {
                Stopwatch sw = Stopwatch.StartNew();
                spec.Process();
                //if (SpectrogamImageSource != null)
                spec.SetFixedWidth(1024);// (int)SpectrogamImageSource.Width);

                Bitmap bmpSpec = new Bitmap(spec.Width, spec.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                using (var bmpSpecIndexed = spec.GetBitmap(multiplier, Decibels, Roll))
                    using (var gfx = Graphics.FromImage(bmpSpec))
                        using (var pen = new System.Drawing.Pen(System.Drawing.Color.White))
                        {
                            gfx.DrawImage(bmpSpecIndexed, 0, 0);
                            if (Roll)
                            {
                                gfx.DrawLine(pen, spec.NextColumnIndex, 0, spec.NextColumnIndex, SpectrogramHeight);
                            }
                        }
                sw.Stop();

                //SpectrogamImageSource.Dispose();
                SpectrogamImageSource    = ImageHelpers.BitmapToImageSource(bmpSpec);
                VerticalScaleImageSource = ImageHelpers.BitmapToImageSource(spec.GetVerticalScale(75));

                RenderTime = $"Render time: {sw.ElapsedMilliseconds:D2} ms";
                Peak       = $"Peak (Hz): {spec.GetPeak().freqHz:N0}";
            }

            TotalTime     = $"Time: {listener.TotalTimeSec:N3} sec";
            FftsProcessed = $"FFTs processed: {spec.FftsProcessed:N0}";

            //Default max on the progressbar is 100 so hardcoding it here for now
            Amplitude = (int)(listener.AmplitudeFrac * 100);

            SpectrogramHeight = spec.Height;

            VerticalScaleImageSource = null;
            VerticalScaleImageSource = ImageHelpers.BitmapToImageSource(spec.GetVerticalScale(LEGEND_WIDTH));
        }
예제 #4
0
        private void timerUpdateSpectrogram_Tick(object sender, EventArgs e)
        {
            if (spec is null)
            {
                return;
            }

            // capture new FFTs and note their indexes
            int fftsBefore = spec.FftsProcessed;

            spec.Add(audioControl1.listener.GetNewAudio());
            int fftsAfter   = spec.FftsProcessed;
            int newFftCount = fftsAfter - fftsBefore;

            int[] newFftIndexes = Enumerable.Range(spec.Width - newFftCount - 1, newFftCount).ToArray();
            var   ffts          = spec.GetFFTs();

            // optionally apply some sort of filter or AGC
            if (settings.agcMode == 1)
            {
                foreach (int fftIndex in newFftIndexes)
                {
                    ffts[fftIndex] = AGC.Method1_SortedFloorMean(ffts[fftIndex], power: settings.agcPower);
                }
            }
            else if (settings.agcMode == 2)
            {
                foreach (int fftIndex in newFftIndexes)
                {
                    ffts[fftIndex] = AGC.Method2_QuicksortFloor(ffts[fftIndex], power: settings.agcPower);
                }
            }

            // display the annotated spectrogram
            var spotsToShow = spots.Where(x => x.ageSec < (11 * 60)).ToList();

            Annotate.Spectrogram(spec, band, spotsToShow, bmpSpectrogram, bmpVericalScale, cbBands.Checked, true, settings);
            pictureBox1.Refresh();
            GC.Collect();
        }
예제 #5
0
        private async Task audio2spectrum_saveVideoAsync(string path)
        {
            var progressHandler = new Progress <string>(value =>
            {
                status_txt.Text = value;
            });
            var progress = progressHandler as IProgress <string>;
            await Task.Run(() =>
            {
                long frameCount;
                try
                {
                    double samplesPerFrame = ((double)audio2spectrum_sampleRate) * frameRate.Denominator / frameRate.Numerator;
                    double totalFrameCount = Math.Ceiling(audio2spectrum_samples.Length / samplesPerFrame);

                    int roundedSamplesPerFrame = (int)Math.Ceiling(samplesPerFrame);

                    int outputWidth        = 50;
                    int outputHeight       = 50;
                    double samplesPerPixel = samplesPerFrame / outputWidth;

                    // Now find closest fft size (take next highest)
                    int fftSize = (int)Math.Pow(2, Math.Ceiling(Math.Log(samplesPerPixel, 2.0)));


                    progress.Report("Audio2Spectrum: Loading spectrogram library");
                    Spectrogram.Spectrogram spec = new Spectrogram.Spectrogram(audio2spectrum_sampleRate, fftSize: 2048, stepSize: 50);


                    spec.SetFixedWidth(outputWidth);
                    //outputWidth = spec.Width;

                    progress.Report("Audio2Spectrum: Initializing video writer");
                    VideoFileWriter writer = new VideoFileWriter();
                    writer.Open(path, outputWidth, outputHeight, frameRate, VideoCodec.FFV1);

                    /*
                     * Console.WriteLine("width:  " + reader.Width);
                     * Console.WriteLine("height: " + reader.Height);
                     * Console.WriteLine("fps:    " + reader.FrameRate);
                     * Console.WriteLine("codec:  " + reader.CodecName);
                     * Console.WriteLine("length:  " + reader.FrameCount);
                     */


                    frameCount = (long)totalFrameCount;

                    // Enlarge the array to make sure we don't end up accessing nonexisting samples. We make it a tiny bit bigger than maybe necessary, just to be safe. (To be honest, I am just too lazy to calculate the precise number we need)

                    /*if((long)Math.Ceiling(frameCount  * samplesPerFrame) > audio2spectrum_samples.Length)
                     * {
                     *  progress.Report("Audio2Spectrum: Resizing array");
                     *  Array.Resize<double>(ref audio2spectrum_samples, (int)Math.Ceiling(frameCount * samplesPerFrame));
                     * }*/

                    double[] frameSampleBuffer = new double[roundedSamplesPerFrame];

                    int currentFrame        = 0;
                    long currentStartSample = 0;

                    progress.Report("Audio2Spectrum: Starting video generation");

                    Bitmap tmp;
                    for (int i = 0; i < frameCount; i++)
                    {
                        currentStartSample = (long)Math.Floor(i *samplesPerFrame);

                        // Doing this branching here now because the resizing the array first was just way way too slow and memory hungry
                        if (currentStartSample >= audio2spectrum_samples.Length) // Even the first sample is already outside the bounds, just make empty array.
                        {
                            frameSampleBuffer = new double[roundedSamplesPerFrame];
                        }
                        else if ((currentStartSample + (roundedSamplesPerFrame - 1)) > (audio2spectrum_samples.Length - 1)) // Copy as many samples as possible
                        {
                            long difference   = (currentStartSample + (roundedSamplesPerFrame - 1)) - (audio2spectrum_samples.Length - 1);
                            frameSampleBuffer = new double[roundedSamplesPerFrame];
                            Array.Copy(audio2spectrum_samples, currentStartSample, frameSampleBuffer, 0, roundedSamplesPerFrame - difference);
                        }
                        else
                        {
                            Array.Copy(audio2spectrum_samples, currentStartSample, frameSampleBuffer, 0, roundedSamplesPerFrame);
                        }

                        spec.Add(frameSampleBuffer);
                        tmp = spec.GetBitmapMel(dB: true, melBinCount: outputHeight);
#if DEBUG
                        Console.WriteLine(tmp.Width + "x" + tmp.Height);
#endif
                        writer.WriteVideoFrame(tmp);
                        if (currentFrame % 1000 == 0)
                        {
                            progress.Report("Audio2Spectrum: Saving video: " + i + "/" + frameCount + " frames");
                        }
                    }

                    writer.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            });

            status_txt.Text = "Audio2Spectrum: Completed saving video.";
            videoIsLoaded   = true;
        }
예제 #6
0
        public async Task <ActionResult> GetPredictionAsync([FromForm] string page, [FromForm] IFormFile audioFile)
        {
            try
            {
                var modelPath = "";

                if (page == "Login")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelLogin.zip");
                }
                else if (page == "Language")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelLanguage.zip");
                }
                else if (page == "Home")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelHome.zip");
                }
                else if (page == "cart")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelCartPage.zip");
                }
                else if (page == "singleItem")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelSingleItemPage.zip");
                }
                else if (page == "category")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelCategory.zip");
                }
                else if (page == "pay")
                {
                    //var v = @"C:\Users\Frank\source\repos\YorubaModelML\YorubaPredictionAPI\Models\MLModelPayPage.zip";
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelPayPage.zip");
                }



                var model = new ConsumeModel(modelPath);

                var uploadPath = Path.Combine(_env.ContentRootPath, "uploads");
                Directory.CreateDirectory(uploadPath);

                if (audioFile.Length > 0)
                {
                    var audioFilePath = Path.Combine(uploadPath, audioFile.FileName);

                    using (var fs = new FileStream(audioFilePath, FileMode.Create))
                    {
                        await audioFile.CopyToAsync(fs);
                    }

                    double[] audio;
                    int      sampleRate;
                    using (var audioFileReader = new AudioFileReader(audioFilePath))
                    {
                        sampleRate = audioFileReader.WaveFormat.SampleRate;
                        var wholeFile   = new List <float>((int)(audioFileReader.Length / 4));
                        var readBuffer  = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
                        int samplesRead = 0;
                        while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            wholeFile.AddRange(readBuffer.Take(samplesRead));
                        }
                        audio = Array.ConvertAll(wholeFile.ToArray(), x => (double)x);
                    }

                    int fftSize = 8192;
                    var spec    = new Spectrogram.Spectrogram(sampleRate, 4096, stepSize: 500, maxFreq: 3000, fixedWidth: 250);
                    spec.Add(audio);
                    var info      = new FileInfo(audioFilePath);
                    var imagepath = Path.Combine(uploadPath, info.Name + ".png");
                    spec.SaveImage(imagepath, intensity: 20_000);


                    var md = new ModelInput {
                        ImageSource = imagepath
                    };
                    var result = model.Predict(md);
                    Directory.Delete(uploadPath, true);
                    return(Ok(new { class_id = result.Prediction, probability = result.Score.Max() }));
                    //return Ok(_env.ContentRootPath);
                }
                _logger.LogError("File is Null");
                return(BadRequest("File is null"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(ex.Message));
            }
        }