Ejemplo n.º 1
0
        private void ResetSpectrogram()
        {
            int sampleRate          = audioControl1.SampleRate;
            int fftSize             = 1 << 14;
            int samplesInTenMinutes = sampleRate * 60 * 10;
            int targetWidth         = settings.targetWidth;
            int stepSize            = samplesInTenMinutes / targetWidth;

            spec = new Spectrogram.Spectrogram(sampleRate, fftSize, stepSize, fixedWidth: targetWidth);
            spec.SetColormap(cmaps.Where(x => x.Name == settings.colormap).First());

            // reset the spectrogram based on where we are in the 10 minute block
            ResetSpectrogramPosition();

            // resize the image based on the spectrogram dimensions
            bmpSpectrogram = new Bitmap(
                width: spec.Width + verticalScaleWidth,
                height: spec.Height / settings.verticalReduction,
                format: PixelFormat.Format32bppPArgb);
            pictureBox1.Image = bmpSpectrogram;
            pictureBox1.Size  = pictureBox1.Image.Size;
            Width             = pictureBox1.Width + 33;
            UpdateVerticalScale();

            Status($"Spectrogram reset");
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
 public SFF(Spectrogram spec, int melBinCount = 0)
 {
     SampleRate    = spec.SampleRate;
     StepSize      = spec.StepSize;
     Width         = spec.Width;
     FftSize       = spec.FftSize;
     FftFirstIndex = spec.NextColumnIndex;
     Height        = spec.Height;
     OffsetHz      = spec.OffsetHz;
     MelBinCount   = melBinCount;
     Ffts          = (melBinCount > 0) ? spec.GetMelFFTs(melBinCount) : spec.GetFFTs();
     CalculateTimes();
     CalculateFrequencies();
 }
Ejemplo n.º 4
0
        //Sets up a listener for the selected microphone and initializes a spectrogram display
        public void StartListening()
        {
            int sampleRate = Int32.Parse(sampleRates[cbSampleRate.SelectedIndex]);
            int fftSize    = 1 << (9 + cbFFTsize.SelectedIndex);
            int stepSize   = fftSize - (int)(fftSize * overlap);

            listener?.Dispose();
            listener = new Listener(cbMicInput.SelectedIndex, sampleRate);
            spec     = new Spectrogram.Spectrogram(sampleRate, fftSize, stepSize);
            if (cmaps != null)
            {
                spec.SetColormap(cmaps[cbCmaps.SelectedIndex]);               //todo this is a hack for this running before SpecInit
            }
            stft = new FFTs(spec.GetFFTs(), spec.SampleRate, spec.StepSize, spec.GetWindow());
        }
Ejemplo n.º 5
0
        private void btnConectionPort_Click(object sender, EventArgs e)
        {
            //if (this.logger is null)
            //{
            //    string comPort = this.cmbComPort.SelectedItem.ToString();
            //    this.logger = new SerialDataLogger(comPort, channelCount: 1);
            //    this.btnConectionPort.Text = "Close";
            //}
            //else
            //{
            //    this.logger.Dispose();
            //    this.logger = null;
            //    this.cmbComPort.Text = "Open";
            //}
#if DEBUG
            if (this.btnConectionPort.Text == "Open")
            {
                string comPort = this.cmbComPort.SelectedItem.ToString();
                this.ssd = new SerialSignalData(comPort, baudRate: 115200);
                this.btnConectionPort.Text             = "Close";
                this.tmrTimeSeriesGraphRender.Enabled  = true;
                this.tmrSpectrogramGraphRender.Enabled = true;

                this.Notch50 = new IIRFilter(int.Parse(this.cmbSampleRate.SelectedItem.ToString()), 50, 5);

                this.spectrogram          = new Spectrogram.Spectrogram(128, 128, 25);
                this.pbSpectrogram.Height = this.spectrogram.Height;

                this.pbScaleVert.Image?.Dispose();
                this.pbScaleVert.Image  = this.spectrogram.GetVerticalScale(this.pbScaleVert.Width);
                this.pbScaleVert.Height = this.spectrogram.Height;
            }
            else
            {
                this.btnConectionPort.Text             = "Open";
                this.tmrTimeSeriesGraphRender.Enabled  = false;
                this.tmrSpectrogramGraphRender.Enabled = false;
            }
#else
            Console.WriteLine("Mode=Other");
#endif
        }
Ejemplo n.º 6
0
        public void Start(int deviceIndex = 0, int fftPower = 14, string preLoadWavFile = null)
        {
            int sampleRate = 8000;
            int fftSize    = (int)(Math.Pow(2, fftPower));

            int tenMinutePixelWidth = 1000;
            int samplesInTenMinutes = sampleRate * 10 * 60;
            int segmentSize         = samplesInTenMinutes / tenMinutePixelWidth;

            spec = new Spectrogram.Spectrogram(sampleRate, fftSize, segmentSize);
            spec.displaySettings.brightness     = 5;
            spec.displaySettings.freqLow        = 1000;
            spec.displaySettings.freqHigh       = 1500;
            spec.displaySettings.tickSpacingHz  = 50;
            spec.displaySettings.tickSpacingSec = 30;

            formBrightness = new FormBrightness(spec);
            formFreqRange  = new FormFreqRange(spec);
            formFFT        = new FormFFT(spec);

            pbSpec.Width  = tenMinutePixelWidth;
            pbSpec.Height = spec.displaySettings.height;

            if (preLoadWavFile != null)
            {
                spec.AddExtend(Tools.ReadWav(preLoadWavFile));
            }
            else
            {
                int bitRate            = 16;
                int channels           = 1;
                int bufferMilliseconds = 20;
                wvin = new NAudio.Wave.WaveInEvent();
                wvin.DeviceNumber       = deviceIndex;
                wvin.WaveFormat         = new NAudio.Wave.WaveFormat(sampleRate, bitRate, channels);
                wvin.DataAvailable     += OnDataAvailable;
                wvin.BufferMilliseconds = bufferMilliseconds;
                wvin.StartRecording();
            }
        }
Ejemplo n.º 7
0
        private void StartListening()
        {
            try
            {
                int sampleRate = 6000;
                int fftSize    = SelectedFftSize;
                int stepSize   = fftSize / 20;

                SpectrogamImageSource = null;
                listener?.Dispose();
                listener = new Listener(DeviceCapabilities.IndexOf(selectedDevice), sampleRate);

                spec = new Spectrogram.Spectrogram(sampleRate, fftSize, stepSize);
                spec.SetWindow(FftSharp.Window.Rectangular(fftSize));
                SpectrogramHeight = spec.Height;

                VerticalScaleImageSource = null;
                VerticalScaleImageSource = ImageHelpers.BitmapToImageSource(spec.GetVerticalScale(LEGEND_WIDTH));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exeption when attempting to start to listen: " + ex.Message);
            }
        }
Ejemplo n.º 8
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;
        }
Ejemplo n.º 9
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));
            }
        }