Beispiel #1
0
        public AudioIn(int sampleRate, int device, int bufferSize)
        {
            if (device > WaveIn.DeviceCount)
            {
                throw new Exception();
            }

            this.sampleRate = sampleRate;
            this.bufferSize = bufferSize;
            audioDataTrue   = new short[bufferSize + 20286];
            tempOdd         = new double[audioDataTrue.Length];
            tempTrue        = new double[audioDataTrue.Length];

            realIn = new PinnedArray <double>(audioDataTrue.Length);
            comOut = new FftwArrayComplex(DFT.GetComplexBufferSize(realIn.GetSize()));
            fft    = FftwPlanRC.Create(realIn, comOut, DftDirection.Forwards);

            waveInDevices            = WaveIn.DeviceCount;
            waveEvent                = new WaveInEvent();
            waveEvent.DeviceNumber   = device;
            waveEvent.DataAvailable += OnWaveIn;
            channels             = WaveIn.GetCapabilities(device).Channels;
            waveEvent.WaveFormat = new WaveFormat(sampleRate, 1);

            string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/NetworkSave01.json";

            // Load Neural network
            if (File.Exists(path))
            {
                Console.WriteLine("exists");
                network = NeuralNetwork.LoadNetwork(path);
            }
            else
            {
                throw new FileNotFoundException();
            }

            //network.PrintWeights();

            waveEvent.StartRecording();
        }
        static void NetworkTraining(int inputSize, int width, int height, int itterations)
        {
            int outputSize = DataSample.labelSize;

            string databasePath1 = @"C:/Users/Mikkel/Documents/MTA18434_SemesterProject/ML_Sound_Samples/Assets/Resources/SampleDatabase/Database.json";

            string networkPath1 = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/NetworkSave01.json";


            if (File.Exists(networkPath1))
            {
                network = NeuralNetwork.LoadNetwork(networkPath1);

                if (network.inputSize != inputSize)
                {
                    throw new Exception();
                }

                if (network.hiddenDimension != width)
                {
                    throw new Exception();
                }

                if (network.hiddenSize != height)
                {
                    throw new Exception();
                }
            }
            else
            {
                Console.WriteLine("Initializing NN");
                network = new NeuralNetwork(inputSize, width, height, outputSize);
                Console.WriteLine("NN Initialized");
            }

            if (File.Exists(databasePath1))
            {
                Console.WriteLine(File.Exists(databasePath1) ? "Database exists." : "File does not exist.");

                SampleDatabase temp = null;

                using (StreamReader r = new StreamReader("C:/Users/Mikkel/Documents/MTA18434_SemesterProject/ML_Sound_Samples/Assets/Resources/SampleDatabase/Database.json"))
                {
                    using (JsonReader reader = new JsonTextReader(r))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        Console.WriteLine("Deserializing");
                        temp = serializer.Deserialize <SampleDatabase>(reader);
                    }
                }

                Console.WriteLine(temp.database[0].data.Length);

                DataSample[] trainingSamples = new DataSample[10];
                Random       rand            = new Random();

                for (int i = 0; i < itterations; i++)
                {
                    Console.WriteLine("Progress: " + i + " / " + itterations);
                    // pick 10 samples
                    for (int j = 0; j < 10; j++)
                    {
                        int num = rand.Next(0, temp.database.Length);
                        trainingSamples[j] = new DataSample(temp.database[num].data, temp.database[num].label);

                        //Console.WriteLine("Database sample " + temp.database[num].data[0] + " " + temp.database[num].label);
                    }

                    network.TrainNetwork(trainingSamples);
                }

                Console.WriteLine("Saving network");
                network.SaveNetwork(networkPath1);
                Console.WriteLine("Network saved");
            }
            else
            {
                Console.WriteLine(File.Exists(databasePath1) ? "File exists." : "File does not exist.");
                throw new Exception();
            }
        }