Пример #1
0
 public void StartRecording(IAudioMediaAsset asset)
 {
     m_sFileName = GetFileName();
     OldAsset    = new AudioMediaAsset(asset.Path);
     Writer      = new BinaryWriter(File.OpenWrite(m_sFileName));
     CreateRIFF(Writer, m_sFileName);
     InitRecording(true);
 }
Пример #2
0
        internal void test()
        {
            AudioMediaAsset am = new AudioMediaAsset("c:\\atest\\a\\Num.wav");
            object          o  = am;
            MediaAsset      m  = o as MediaAsset;

//m_htAssetList.Add  (1 , am) ;
            MessageBox.Show(m.GetType().ToString());

            MessageBox.Show("It is working");
        }
Пример #3
0
        //it will start actual recording, append if there is data
        //in the wave file through the RecordCaptureData()
        public void InitRecording(bool SRecording)
        {
            //if no device is set then it is informed then no device is set
            if (null == m_cApplicationDevice)
            {
                throw new Exception("no device is set for recording");
            }
            //format of the capture buffer and the input format is compared
            //if not same then it is informed that formats do not match

            if (dsc.Format.ToString() != InputFormat.ToString())
            {
                throw new Exception("formats do not match");
            }

            if (SRecording)
            {
                StateChanged mStateChanged = new StateChanged(mState);
                mState = AudioRecorderState.Recording;
                FireEvent(mStateChanged);
                CreateCaptureBuffer(Index);
                applicationBuffer.Start(true);                //it will set the looping till the stop is used
            }
            else
            {
                applicationBuffer.Stop();
                RecordCapturedData();
                Writer = new BinaryWriter(File.OpenWrite(m_sFileName));
                long Audiolength        = (long)(SampleCount + 44);
                CalculationFunctions cf = new CalculationFunctions();
                for (int i = 0; i < 4; i++)
                {
                    Writer.BaseStream.Position = i + 4;
                    Writer.Write(Convert.ToByte(cf.ConvertFromDecimal(Audiolength)[i]));
                }
                for (int i = 0; i < 4; i++)
                {
                    Writer.BaseStream.Position = i + 40;
                    Writer.Write(Convert.ToByte(cf.ConvertFromDecimal(SampleCount)[i]));
                }
                Writer.Close();                 // Close the file now.
                Writer = null;                  // Set the writer to null.
                //m_AudioMediaAsset = new AudioMediaAsset(ProjectDirectory+"\\"+m_sFileName);
                m_AudioMediaAsset = new AudioMediaAsset(m_sFileName);
                if (OldAsset.SizeInBytes > 44)
                {
                    OldAsset.MergeWith(m_AudioMediaAsset);
                    //m_AudioMediaAsset = OldAsset;
                }
            }
        }
Пример #4
0
        public ArrayList ApplyPhraseDetection(long threshold, long length, long before)
        {
            ArrayList       alByteMarks     = new ArrayList(DetectPhrases(threshold, length, before));
            ArrayList       ReturnArrayList = new ArrayList();
            string          FileName;
            AudioMediaAsset am;

            BinaryWriter bw;

            if (alByteMarks != null)
            {
/*
 * // make a physical asset for first phrase
 *                              FileName = GenerateFileName ( ".wav" , m_sDirPath ) ;
 *                              bw = new BinaryWriter (File.Create (FileName)) ;
 *                                      bw.Write (this.GetChunk ( Convert.ToInt64 (0) , Convert.ToInt64 (alByteMarks[0]) ) ) ;
 *                              bw.Close () ;
 *                              am = new AudioMediaAsset (FileName) ;
 *                              ReturnArrayList.Add (am) ;
 */
// for array following
                int Count = alByteMarks.Count;
                for (int i = 0; i < Count; i++)

                {
                    FileName = GenerateFileName(".wav", m_sDirPath);

                    bw = new BinaryWriter(File.Create(FileName));
                    //am = new AudioMediaAsset (FileName) ;

                    if (i < Count - 1)
                    {
                        bw.Write(this.GetChunk(Convert.ToInt64(alByteMarks[i]), Convert.ToInt64(alByteMarks[i + 1])));
                    }
                    else
                    {
                        bw.Write(this.GetChunk(Convert.ToInt64(alByteMarks[i]), this.AudioLengthBytes));
                    }

                    bw.Close();

                    am = new AudioMediaAsset(FileName);
                    ReturnArrayList.Add(am);
                }
            }

            ArrayList a = new ArrayList();

            return(ReturnArrayList);
        }
Пример #5
0
        public void StartListening(IAudioMediaAsset asset)
        {
            StateChanged mStateChanged = new StateChanged(mState);

            mState = AudioRecorderState.Listening;
            FireEvent(mStateChanged);
            //m_AudioMediaAsset = new AudioMediaAsset(asset.Path);
//			m_AudioMediaAsset = asset;
            m_sFileName = GetFileName();
            OldAsset    = new AudioMediaAsset(asset.Path);
            Writer      = new BinaryWriter(File.OpenWrite(m_sFileName));
            CreateRIFF(Writer, m_sFileName);
            InitRecording(true);
        }
Пример #6
0
        public IAudioMediaAsset MergeWith(IAudioMediaAsset next)
        {
            AudioMediaAsset amNext = new AudioMediaAsset(next.Path);

            string       sTempPath = GenerateFileName(".wav", m_sDirPath);
            BinaryWriter br        = new BinaryWriter(File.Create(sTempPath));

            br.Write(GetChunk(0, this.AudioLengthBytes));
            br.Close();

            AudioMediaAsset am1 = new AudioMediaAsset(sTempPath);

            am1.InsertByteBuffer(amNext.GetChunk(0, amNext.AudioLengthBytes), this.AudioLengthBytes - 1);


            return(am1);
        }
Пример #7
0
//create a new  file in project directory and copy contents of source file to it.
        public IMediaAsset CopyAsset(IMediaAsset asset)
        {
            FileInfo file  = new FileInfo(asset.Path);
            string   sTemp = GenerateFileName(file.Extension, m_sDirPath);

            try
            {
                file.CopyTo(sTemp, false);
            }
            catch
            {
                MessageBox.Show("Can not copy the file");
                return(null);
            }
            AudioMediaAsset ob = new AudioMediaAsset(sTemp);

            m_htAssetList.Add(ob.Name, ob);
            return(ob);
        }
Пример #8
0
        //create local asset from some external asset
        public IMediaAsset AddAsset(TypeOfMedia assetType, string assetPath)
        {
            string sTemp = null;

            if (assetType.Equals(TypeOfMedia.Audio))
            {
                sTemp = GenerateFileName(".wav", m_sDirPath);
            }


            if (sTemp != null)
            {
                try
                {
                    //File.Create (sTemp) ;
                    File.Copy(assetPath, sTemp, true);
                }
                catch
                {
                    MessageBox.Show("Exeption! file could not be created in add asset in asset manager");
                }

                AudioMediaAsset am = new AudioMediaAsset(sTemp);

                try
                {
                    m_htAssetList.Add(am.Name, am);
                }
                catch
                {
                    MessageBox.Show("Exeption! can not add duplicate in hash table");
                }
                return(am);
            }
            else
            {
                MessageBox.Show("Asset could not be created and added. A null exeption will be thrown");
                return(null);
            }
// end of function
        }
Пример #9
0
        //End Validate


        public ArrayList Split(long position)
        {
            if (m_AudioLengthBytes > position)
            {
                position = this.AdaptToFrame(position);
                string       sTempPath = GenerateFileName(".wav", m_sDirPath);
                BinaryWriter br        = new BinaryWriter(File.Create(sTempPath));

                br.Write(GetChunk(0, position));
                br.Close();

                AudioMediaAsset am1 = new AudioMediaAsset(sTempPath);

                string sTempPath2 = GenerateFileName(".wav", m_sDirPath);
                //MessageBox.Show ("About to create second file") ;
                br = new BinaryWriter(File.Create(sTempPath2));

                br.Write(GetChunk(position, m_AudioLengthBytes));
                br.Close();

                AudioMediaAsset am2 = new AudioMediaAsset(sTempPath2);
                //MessageBox.Show ("Deleting and renaming") ;


                // Create ArrayList of Files and return
                ArrayList alReturn = new ArrayList();
                alReturn.Add(am1);
                alReturn.Add(am2);

                return(alReturn);
            }
            else
            {
                return(null);
            }

// end of function
        }
Пример #10
0
        //string type must contain valid extension like .wav
        // Parameter changed to enum TypeOfMedia
        public IMediaAsset NewAsset(TypeOfMedia assetType)
        {
            string sTemp = null;

            if (assetType.Equals(TypeOfMedia.Audio))
            {
                sTemp = GenerateFileName(".wav", m_sDirPath);
            }

            if (sTemp != null)
            {
                //File.Create (sTemp) ;
                BinaryWriter bw = new BinaryWriter(File.Create(sTemp));



                bw.BaseStream.Position = 0;
                byte b = Convert.ToByte(1);
                for (int i = 0; i < 44; i++)
                {
                    bw.Write(b);
                }
                b = Convert.ToByte(44);
                bw.BaseStream.Position = 7;
                bw.Write(b);
                bw.Close();

                AudioMediaAsset am = new AudioMediaAsset(sTemp);
                m_htAssetList.Add(am.Name, am);
                return(am);
            }
            else
            {
                MessageBox.Show("File could not be created and newAsset will throw a null exeption");
                return(null);
            }
        }
Пример #11
0
        // The function opens two files (target file and source file)simultaneously  and copies from one to other frame by frame
        //This is done to reduce load on system memory as wave file may be as big as it go out of scope of RAM
        // IAudioMediaAsset Target is destination file where the stream has to be inserted and  TargetBytePos is insertion position in bytes excluding header
        //IAudioMediaAsset Source is asset from which data is taken between Positions in bytes (StartPos and EndPos) , these are also excluding header length
        internal void InsertAudio(long TargetBytePos, IAudioMediaAsset ISource, long StartPos, long EndPos)
        {
            AudioMediaAsset Source = new AudioMediaAsset(ISource.Path);

            // checks the compatibility of formats of two assets and validity of parameters
            if (CheckStreamsFormat(Source) == true && TargetBytePos < (m_AudioLengthBytes) && StartPos < EndPos && EndPos < Source.AudioLengthBytes)
            // braces holds whole  function
            {
                // opens Target asset and Source asset for reading and create temp file for manipulation
                BinaryReader brTarget = new BinaryReader(File.OpenRead(m_sFilePath));

                BinaryReader brSource = new BinaryReader(File.OpenRead(Source.Path));

                BinaryWriter bw = new BinaryWriter(File.Create(m_sFilePath + "tmp"));

                // adapt positions to frame size
                TargetBytePos = AdaptToFrame(TargetBytePos) + 44;
                StartPos      = Source.AdaptToFrame(StartPos) + 44;
                EndPos        = Source.AdaptToFrame(EndPos) + 44;
                int Step = FrameSize;

                // copies audio stream before the insertion point into temp file
                bw.BaseStream.Position       = 0;
                brTarget.BaseStream.Position = 0;
                long lCount = TargetBytePos;
                long i      = 0;
                for (i = 0; i < lCount; i = i + Step)
                {
                    bw.Write(brTarget.ReadBytes(Step));
                }

                // copies the audio stream data (between marked positions) from scource file  to temp file
                brSource.BaseStream.Position = StartPos;
                lCount = lCount + (EndPos - StartPos);
                for (i = i; i < lCount; i = i + Step)
                {
                    bw.Write(brSource.ReadBytes(Step));
                }

                // copies the remaining data after insertion point in Target asset into temp file
                FileInfo file = new FileInfo(m_sFilePath);
                lCount = file.Length - 44 + (EndPos - StartPos);

                brTarget.BaseStream.Position = TargetBytePos;

                for (i = i; i < lCount; i = i + Step)
                {
                    try
                    {
                        bw.Write(brTarget.ReadBytes(Step));
                    }
                    catch
                    {
                        MessageBox.Show("Problem   " + i);
                    }
                }


                // close all the reading and writing objects
                brTarget.Close();
                brSource.Close();
                bw.Close();

                //  Delete the target file and rename the temp file to target file
                FileInfo pfile = new FileInfo(m_sFilePath);
                pfile.Delete();

                FileInfo nfile = new FileInfo(m_sFilePath + "tmp");
                nfile.MoveTo(m_sFilePath);


                // Update lenth fields in header of temp file and also members of Target asset
                FileInfo filesize = new FileInfo(m_sFilePath);
                m_lSize = filesize.Length;

                BinaryWriter bw1 = new BinaryWriter(File.OpenWrite(m_sFilePath));
                UpdateLengthHeader(m_lSize, bw1);
                bw1.Close();
            }
            else
            {
                MessageBox.Show("Can not manipulate. files are of different format or invalid input parameters passed");
            }

            // End insert  function
        }