public void OutputIteration(int song, EmotionAnalysis analysis, EmotionVector vector)
        {
            using (StreamWriter sw = new StreamWriter(file, true))
            {
                sw.WriteLine(iterations + ";" + song + ";" + analysis.Emotion + ";" + analysis.Happy + ";" + analysis.Sad + ";" + analysis.Anger + ";" + analysis.Fear + ";" + analysis.Neutral + ";" + vector.Arousal + ";" + vector.Valence);
            }

            iterations++;
        }
        public EmotionVector CalculateVector(EmotionAnalysis analysis)
        {
            double wMinX = minX, wMinY = minY, wMaxX = maxX, wMaxY = maxY;
            double xMid, yMid, xStep, yStep, current, best;

            for (int i = 0; i < iterations; i++)
            {
                xStep = (wMaxX - wMinX) / 4;
                xMid = wMinX + xStep;
                best = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint(wMinX + xStep, (wMaxY + wMinY) / 2));

                current = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint(wMinX + xStep * 2, (wMaxY + wMinY) / 2));
                if (current < best)
                {
                    best = current;
                    xMid = wMinX + xStep * 2;
                }

                current = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint(wMinX + xStep * 3, (wMaxY + wMinY) / 2));
                if (current < best)
                {
                    best = current;
                    xMid = wMinX + xStep * 3;
                }

                yStep = (wMaxY - wMinY) / 4;
                yMid = wMinY + yStep;
                best = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint((wMaxX + wMinX) / 2, wMinY + yStep));

                current = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint((wMaxX + wMinX) / 2, wMinY + yStep * 2));
                if (current < best)
                {
                    best = current;
                    yMid = wMinY + yStep * 2;
                }

                current = DistanceBetweenEmotions(analysis, CalculateDistanceToPoint((wMaxX + wMinX) / 2, wMinY + yStep * 3));
                if (current < best)
                {
                    best = current;
                    yMid = wMinY + yStep * 3;
                }

                wMinX = xMid - xStep;
                wMaxX = xMid + xStep;
                wMinY = yMid - yStep;
                wMaxY = yMid + yStep;
            }

            return new EmotionVector((wMinX + wMaxX) / 2, (wMinY + wMaxY) / 2);
        }
        private void stopRecordingButton_Click(object sender, EventArgs e)
        {
            currentAnalysis = dbm.SearchDatabaseForEmotion(windows);
            currentVector = ec.CalculateVector(currentAnalysis);

            try
            {
                waveIn.StopRecording();
                waveIn.Dispose();
                waveIn = null;
                waveChart.Series[0].Points.Clear();
            }
            catch { }

            stopRecordingButton.Enabled = false;
            startRecordingButton.Enabled = true;

            this.musicPlayer = new MusicPlayer(db.CreatePlaylist(currentVector, settings.GoalEmotion, settings.GenrePreferences));
        }
        /// <summary>
        /// Searches in the corresponding databse for the emotional state of the user.
        /// </summary>
        /// <param name="measurements"> A WindowEmotion measured from the user's speech.</param>
        /// <returns>The emotional state of the user.</returns>
        public EmotionAnalysis SearchDatabaseForEmotion(List<Window> windows)
        {
            double tHappy = 0.0f, tAnger = 0.0f, tNeutral = 0.0f, tSad = 0.0f, tFear = 0.0f;

            List<WindowEmotion> db = GlobalVariables.Gender == 'm' ? DatabaseM : DatabaseF;

            foreach (Window window in windows)
            {
                double sHappy = Double.MaxValue, sAnger = Double.MaxValue, sNeutral = Double.MaxValue, sSad = Double.MaxValue, sFear = Double.MaxValue;

                foreach(WindowEmotion dataBaseEntry in db)
                {
                    // Calculate distance based on sum of squared errors
                    double averagePitchDistance = window.Measurements.averagePitch - dataBaseEntry.AveragePitch;
                    double pitchSTDDistance = window.Measurements.pitchSTD - dataBaseEntry.PitchSTD;
                    double energySTDDistance = window.Measurements.energySTD - dataBaseEntry.EnergySTD;

                    double currentDistance = Math.Pow(averagePitchDistance / pitchDifference, 2) + Math.Pow(pitchSTDDistance / pitchSTDDifference, 2) + Math.Pow(energySTDDistance / energySTDDifference, 2);

                    switch (dataBaseEntry.Emotion)
                    {
                        case 'H':
                            if (currentDistance < sHappy)
                                sHappy = currentDistance;
                            break;
                        case 'A':
                            if (currentDistance < sAnger)
                                sAnger = currentDistance;
                            break;
                        case 'S':
                            if (currentDistance < sSad)
                                sSad = currentDistance;
                            break;
                        case 'F':
                            if (currentDistance < sFear)
                                sFear = currentDistance;
                            break;
                        default:
                            if (currentDistance < sNeutral)
                                sNeutral = currentDistance;
                            break;
                    }
                }

                tHappy += sHappy;
                tSad += sSad;
                tAnger += sAnger;
                tFear += sFear;
                tNeutral += sNeutral;
            }

            EmotionAnalysis ea = new EmotionAnalysis(tHappy, tSad, tAnger, tFear, tNeutral);
            ea.Emotion = 'N';

            if (tHappy < tNeutral)
            {
                ea.Emotion = 'H';
                tNeutral = tHappy;
            }
            if (tSad < tNeutral)
            {
                ea.Emotion = 'S';
                tNeutral = tSad;
            }
            if (tAnger < tNeutral)
            {
                ea.Emotion = 'A';
                tNeutral = tAnger;
            }
            if (tFear < tNeutral)
            {
                ea.Emotion = 'F';
                tNeutral = tFear;
            }

            System.Diagnostics.Debug.WriteLine("Your emotion is {0}", ea.Emotion);

            return ea;
        }
 private EmotionAnalysis NormalizeAnalysis(EmotionAnalysis analysis)
 {
     double total = analysis.Anger + analysis.Fear + analysis.Happy + analysis.Neutral + analysis.Sad;
     return new EmotionAnalysis(analysis.Happy / total, analysis.Sad / total, analysis.Anger / total, analysis.Fear / total, analysis.Neutral / total);
 }
 private double DistanceBetweenEmotions(EmotionAnalysis one, EmotionAnalysis two)
 {
     EmotionAnalysis a = NormalizeAnalysis(one);
     EmotionAnalysis b = NormalizeAnalysis(two);
     return Math.Abs(a.Anger - b.Anger) + Math.Abs(a.Fear - b.Fear) + Math.Abs(a.Happy - b.Happy) + Math.Abs(a.Neutral - b.Neutral) + Math.Abs(a.Sad - b.Sad);
 }