Exemple #1
0
        // Generate beats
        void PlaceBeats()
        {
            notes.Clear();

            // Screen percentages
            markerPosX = (int)(graphics.PreferredBackBufferWidth * MARKER_POS);
            int halfScreenHeight = graphics.PreferredBackBufferHeight / 2;

            // Go through the list of onsets detected
            for (int itemNo = 0; itemNo < audioAnalysis.GetOnsets().Length; itemNo++)
            {
                // Retrieve the item from the audio analysis onset detected list
                float onset = audioAnalysis.GetOnsets()[itemNo];

                // If the item is an onset
                if (onset > 0.0)
                {
                    GameObject newNote = new GameObject(beatTexture);

                    // Set the origin of the note in the centre
                    newNote.Origin = Vector2.One * beatTexture.Height / 2;

                    // Length of time each item in the onset detected list represents
                    float timePos = audioAnalysis.GetTimePerSample();

                    // Place the note in accordance to its position in the song
                    float xPosition = (itemNo * timePos);
                    // Stretch the timeline so the notes aren't bunch up
                    xPosition *= PIXELS_SECOND;
                    // Add an offset, the marker represents the current position in the song
                    xPosition += markerPosX;

                    // Set the note in the correct position
                    newNote.Position = new Vector2(xPosition, halfScreenHeight);

                    // Set the note moving towards the left
                    newNote.Velocity = new Vector2(-PIXELS_SECOND, 0);

                    if (onset > 0.5f)
                    {
                        newNote.TintColor = Color.Orange;
                    }
                    else
                    {
                        newNote.TintColor = Color.MediumPurple;
                    }


                    notes.Add(newNote);
                }
            }
        }
Exemple #2
0
        public Song(string fileDirectory)
        {
            #region cppAnalyzerOld

            /*var FloatSamples = Utilities.FloatArrayFromAudio(fileDirectory).ToArray();
             * Process audioAnalyzer = new Process();
             * string path = @"AubioAnalyzerCpp.exe";
             * string currDirectory = Directory.GetCurrentDirectory();
             * audioAnalyzer.StartInfo = new ProcessStartInfo(path, '"' + Directory.GetCurrentDirectory() + "\" " + FloatSamples.Length)
             * {
             *      UseShellExecute = false,
             *      RedirectStandardInput = true,
             *      RedirectStandardOutput = true,
             * };
             * audioAnalyzer.Start();
             * foreach (float sample in FloatSamples) {
             *      audioAnalyzer.StandardInput.WriteLine(sample);
             * }
             *
             * string BPMString = audioAnalyzer.StandardOutput.ReadToEnd();
             * BeatsPerMinute = (float)Convert.ToDouble(BPMString);
             * audioAnalyzer.WaitForExit();
             * if (audioAnalyzer.ExitCode != 0) throw new Exception("Audio Analyzer Failed");*/
            #endregion cppAnalyzerOld

            AudioAnalysis analyzer = new AudioAnalysis();
            analyzer.LoadAudioFromFile(fileDirectory);
            analyzer.DetectOnsets(out fluxes);
            //BPMDetector bpmdet = new BPMDetector(fileDirectory);
            //BeatsPerMinute = bpmdet.getBPM();
            //msTilStart = bpmdet.getOffset();
            Onsets        = analyzer.GetOnsets().ToList();
            TimePerSample = analyzer.GetTimePerSample();
        }
        public void Initialize(ISampleSource soundSource, GeneratorOptions options)
        {
            analysis           = new AudioAnalysis();
            analysis.PCMStream = soundSource;
            analysis.DetectOnsets();
            analysis.NormalizeOnsets(0);
            analysis.PCMStream.Position = 0;

            fps = options.Fps;

            Console.WriteLine("Audio analyzer detected " + analysis.GetOnsets().Count(f => f > 0) + " onsets!");
        }
        public void Apply(IRenderer renderer, Frame frame)
        {
            var frameTime   = frame.Time.TotalSeconds;
            var num         = 0;
            var maxTimespan = (1.0d / fps) * maxFrames;

            foreach (var onset in analysis.GetOnsets())
            {
                if (onset > 0.04)
                {
                    float timePos  = num * analysis.GetTimePerSample();
                    var   timeDiff = Math.Abs(frameTime - timePos);
                    if (timeDiff <= maxTimespan)
                    {
                        var onsetMultiplier = Math.Min(1.0, onset + 0.5);
                        var timeProgress    = timeDiff / maxTimespan;
                        renderer.DrawRect(new Rectangle(0, 0, 1920, 1080), Color.FromArgb((int)(255.0f * intensity * timeProgress * onsetMultiplier), 255, 255, 255));
                        return;
                    }
                }
                num++;
            }
        }