public static uint FrequencyRate(double[,] audioWithHeader)
        {
            string currentASCII = "";
            uint   posistion    = 0;

            do
            {
                byte[] wordLooking =
                {
                    (byte)audioWithHeader[posistion,     0],
                    (byte)audioWithHeader[posistion + 1, 0],
                    (byte)audioWithHeader[posistion + 2, 0],
                    (byte)audioWithHeader[posistion + 3, 0]
                };
                currentASCII = Conversion.ByteArrayToASCII(wordLooking);
                posistion++;
            } while (currentASCII != "fmt ");
            uint fmtChunkStartLocation = --posistion;

            byte[] frequencyBytes =
            {
                (byte)audioWithHeader[12 + fmtChunkStartLocation, 0],
                (byte)audioWithHeader[13 + fmtChunkStartLocation, 0],
                (byte)audioWithHeader[14 + fmtChunkStartLocation, 0],
                (byte)audioWithHeader[15 + fmtChunkStartLocation, 0]
            };
            uint value = 0;

            Conversion.ByteConverterToInterger(frequencyBytes, ref value);
            return(value);
        }
        public static short BitsPerSample(byte[] wavefile, uint startLocation)
        {
            byte[] bitsPerSample =
            {
                wavefile[22 + startLocation],
                wavefile[23 + startLocation]
            };
            short value = 0;

            Conversion.ByteConverterToInterger(bitsPerSample, ref value);
            return(value);
        }
        public static ushort ChannelAmount(byte[] wavefile, uint startLocation)
        {
            byte[] channelAmountByte =
            {
                wavefile[10 + startLocation],
                wavefile[11 + startLocation]
            };

            ushort value = 0;

            Conversion.ByteConverterToInterger(channelAmountByte, ref value);
            return(value);
        }
        public static uint DataSectionSize(byte[] wavefile, uint startLocation)
        { //need to check for fact and data chunk section it seems like
            byte[] dataSectionSizeByte =
            {
                wavefile[4 + startLocation],
                wavefile[5 + startLocation],
                wavefile[6 + startLocation],
                wavefile[7 + startLocation]
            };
            uint value = 0;

            Conversion.ByteConverterToInterger(dataSectionSizeByte, ref value);
            return(value);
        }
        public static uint FrequencyRate(byte[] wavefile, uint startLocation)
        {
            byte[] frequencyBytes =
            {
                wavefile[12 + startLocation],
                wavefile[13 + startLocation],
                wavefile[14 + startLocation],
                wavefile[15 + startLocation]
            };
            uint value = 0;

            Conversion.ByteConverterToInterger(frequencyBytes, ref value);
            return(value);
        }
        /// <summary>
        /// Converts an array, <paramref name="wavefile"/>, of bytes to double array.
        /// </summary>
        /// <param name="wavefile">The entire wavefile with the data that need processing. Should also contain the header.</param>
        /// <param name="dataSectionSize">The size of the data section.</param>
        /// <param name="channelAmount">The amount of channels.</param>
        /// <param name="bitsPerSample">The amount of bits per samples.</param>
        /// <returns>Returns an array with the converted data.</returns>
        public static double[,] ByteArrayToTimeDomain(byte[] wavefile, ulong dataSectionSize, ushort channelAmount, short bitsPerSample, ulong waveHeaderEnding)
        {
            double[,] timeDomain = new double[(dataSectionSize) / ((ulong)(bitsPerSample / 8) * channelAmount), channelAmount]; //length of data / ((total amount of bits/bits per byte) * amount of channels) , amount of channels
            ulong postion    = waveHeaderEnding;
            byte  byteAmount = (byte)(bitsPerSample / 8);

            for (int n = 0; n < channelAmount; n++)
            {
                ulong currentPosition    = postion;
                ulong arrayIndexLocation = 0;
                while (currentPosition + byteAmount <= (ulong)(dataSectionSize + 44))
                {
                    byte[] array = new byte[byteAmount];
                    for (uint m = 0; m < byteAmount; m++)
                    {
                        array[m] = wavefile[currentPosition + m];
                    }
                    double endValue = 0;
                    if (bitsPerSample / 8 == 2)
                    {
                        short value = 0;
                        Conversion.ByteConverterToInterger(array, ref value);
                        endValue = Resizer.Resize(value);
                    }
                    else if (bitsPerSample / 8 == 4)
                    {
                        int value = 0;
                        Conversion.ByteConverterToInterger(array, ref value);
                        endValue = Resizer.Resize(value);
                    }
                    else if (bitsPerSample / 8 == 8)
                    {
                        long value = 0;
                        Conversion.ByteConverterToInterger(array, ref value);
                        endValue = Resizer.Resize(value);
                    }
                    else
                    {
                        byte value = array[0];
                        endValue = Resizer.Resize(value);
                    }
                    timeDomain[arrayIndexLocation, n] = endValue;
                    arrayIndexLocation++;
                    currentPosition += (ulong)(byteAmount * channelAmount);
                }
            }
            return(timeDomain);
        }
Beispiel #7
0
        public static void LoadAudio(string signalPathwayAndName)
        {
            byte[]   wav               = WaveClass.LoadAudioFile(signalPathwayAndName);
            string[] seperation        = signalPathwayAndName.Split("\\");
            string[] nameWithFormat    = seperation[seperation.Length - 1].Split(".");
            string   nameWithoutFormat = nameWithFormat[0];
            uint     headerSize        = 44;
            string   currentASCII      = "";
            uint     posistion         = 0;

            do
            {
                byte[] wordLooking =
                {
                    wav[posistion],
                    wav[posistion + 1],
                    wav[posistion + 2],
                    wav[posistion + 3]
                };
                currentASCII = Conversion.ByteArrayToASCII(wordLooking);
                posistion++;
            } while (currentASCII != "fmt ");
            uint fmtChunkStartLocation = --posistion;
            uint mainHeaderChunkSize   = fmtChunkStartLocation;

            byte[] sizeArray = new byte[4]
            {
                wav[fmtChunkStartLocation + 4],//+ 4 to placement because of ID chunk is 4 bytes
                wav[fmtChunkStartLocation + 5],
                wav[fmtChunkStartLocation + 6],
                wav[fmtChunkStartLocation + 7]
            };

            uint fmtChunkSize = 0;

            Conversion.ByteConverterToInterger(sizeArray, ref fmtChunkSize);
            sizeArray = new byte[2]
            {
                wav[fmtChunkStartLocation + 8],
                wav[fmtChunkStartLocation + 9]
            };
            fmtChunkSize += 8;
            uint fmtFormatCode = 0;

            Conversion.ByteConverterToInterger(sizeArray, ref fmtFormatCode);

            uint factChunkStartLocation = 0;
            uint factChuckSize          = 0;
            uint factNumberOfSamples;

            if (fmtFormatCode != 1)
            {
                currentASCII = "";
                posistion    = 8;
                do
                {
                    byte[] wordLooking =
                    {
                        wav[posistion],
                        wav[posistion + 1],
                        wav[posistion + 2],
                        wav[posistion + 3]
                    };
                    currentASCII = Conversion.ByteArrayToASCII(wordLooking);
                    posistion++;
                } while (currentASCII != "fact");
                factChunkStartLocation = --posistion;
            }

            currentASCII = "";
            posistion    = 8;
            do
            {
                byte[] wordLooking =
                {
                    wav[posistion],
                    wav[posistion + 1],
                    wav[posistion + 2],
                    wav[posistion + 3]
                };
                currentASCII = Conversion.ByteArrayToASCII(wordLooking);
                posistion++;
            } while (currentASCII != "data");
            uint dataChunkStartLocation = --posistion;

            headerSize = dataChunkStartLocation > factChunkStartLocation ? dataChunkStartLocation + 8 : factChunkStartLocation + factChuckSize;

            byte[] wavHeader = new byte[headerSize];
            for (int i = 0; i < headerSize; i++)
            {
                wavHeader[i] = wav[i];
            }
            ushort channelAmount   = WaveClass.ChannelAmount(wavHeader, fmtChunkStartLocation);
            uint   dataSegmentSize = WaveClass.DataSectionSize(wavHeader, dataChunkStartLocation);
            short  bitsPerSample   = WaveClass.BitsPerSample(wavHeader, fmtChunkStartLocation);

            double[,] audioScaled     = WaveClass.ByteArrayToTimeDomain(wav, dataSegmentSize, channelAmount, bitsPerSample, (ulong)wavHeader.Length);
            double[,] audioWithHeader = AudioStorageProcessing.AddWaveToSignal(audioScaled, wavHeader);
            Storage.SignalToStorage(audioWithHeader, nameWithoutFormat);
        }