コード例 #1
0
    private void StartWavCut(string sTxtInput, string sWavInput, string sOut)
    {
        if (!File.Exists(sTxtInput) || !File.Exists(sWavInput))
            throw new WavCutException("File does not exist.");

        string[] arLines = File.ReadAllLines(sTxtInput);
        if (arLines == null || arLines.Length < 3)
            throw new WavCutException("not enough lines");

        if (arLines[0] == "wavcut")
        {
        }
        else if (arLines[0] == "wavfindsilence")
        {
            CWavcutFindSilence.FindSilence(sWavInput, arLines);
            return;
        }
        else
        {
            throw new WavCutException("The first line of the input text file should be wavcut, see trombone.txt");
        }

        // read input file
        List<int> arOffsets = new List<int>();
        int nPrevframe = 0;
        for (int i = 1; i < arLines.Length; i++)
        {
            int nFrame;
            if (arLines[i] == "" || arLines[i] == null) continue;
            if (arLines[i].Contains("."))
                throw new WavCutException("We don't support decimals");
            if (!int.TryParse(arLines[i],out nFrame))
                throw new WavCutException("Line" + (i + 1) + " could not parse int");
            
            arOffsets.Add(nFrame);
            if (nFrame <= nPrevframe) throw new WavCutException("samples must be increasing");
            nPrevframe = nFrame;
        }
        if (arOffsets.Count <= 0) throw new WavCutException("Error, no points given");

        // count number of samples
        int nSamples = CCountWaveSamples.CountWaveSamples(sWavInput);
        arOffsets.Add(nSamples);

        // construct objects that specify the intervals
        List<WaveSegment> arSegments = new List<WaveSegment>();
        for (int i = 0; i < arOffsets.Count; i++)
        {
            if (nSamples <= arOffsets[i] && i<arOffsets.Count-1)
                throw new WavCutException("Error, offset is greater than length of file");
            int length = (i == 0) ? arOffsets[i] : arOffsets[i] - arOffsets[i - 1];
            Console.WriteLine("track " + i+ " length(s) "+length/44100.0);
            WaveSegment seg = new WaveSegment();
            seg.m_writer = new CWaveWrite(sOut,i,length);
            seg.m_nStopPoint = arOffsets[i];
            arSegments.Add(seg);
        }

        // stream through the audio
        CCutWaveProcessor processor = new CCutWaveProcessor(arSegments);
        CWavStreamReader.StreamThroughWaveFile(sWavInput,processor);
        if (nSamples != processor.GetNumberSamplesSeen())
            Console.WriteLine("Warning, processor did not see all samples.");

        // save files and close file handles.
        for (int i = 0; i < arSegments.Count; i++)
        {
            arSegments[i].m_writer.Save();
        }
    }
コード例 #2
0
    private void StartWavCut(string sTxtInput, string sWavInput, string sOut)
    {
        if (!File.Exists(sTxtInput) || !File.Exists(sWavInput))
        {
            throw new WavCutException("File does not exist.");
        }

        string[] arLines = File.ReadAllLines(sTxtInput);
        if (arLines == null || arLines.Length < 3)
        {
            throw new WavCutException("not enough lines");
        }

        if (arLines[0] == "wavcut")
        {
        }
        else if (arLines[0] == "wavfindsilence")
        {
            CWavcutFindSilence.FindSilence(sWavInput, arLines);
            return;
        }
        else
        {
            throw new WavCutException("The first line of the input text file should be wavcut, see trombone.txt");
        }

        // read input file
        List <int> arOffsets  = new List <int>();
        int        nPrevframe = 0;

        for (int i = 1; i < arLines.Length; i++)
        {
            int nFrame;
            if (arLines[i] == "" || arLines[i] == null)
            {
                continue;
            }
            if (arLines[i].Contains("."))
            {
                throw new WavCutException("We don't support decimals");
            }
            if (!int.TryParse(arLines[i], out nFrame))
            {
                throw new WavCutException("Line" + (i + 1) + " could not parse int");
            }

            arOffsets.Add(nFrame);
            if (nFrame <= nPrevframe)
            {
                throw new WavCutException("samples must be increasing");
            }
            nPrevframe = nFrame;
        }
        if (arOffsets.Count <= 0)
        {
            throw new WavCutException("Error, no points given");
        }

        // count number of samples
        int nSamples = CCountWaveSamples.CountWaveSamples(sWavInput);

        arOffsets.Add(nSamples);

        // construct objects that specify the intervals
        List <WaveSegment> arSegments = new List <WaveSegment>();

        for (int i = 0; i < arOffsets.Count; i++)
        {
            if (nSamples <= arOffsets[i] && i < arOffsets.Count - 1)
            {
                throw new WavCutException("Error, offset is greater than length of file");
            }
            int length = (i == 0) ? arOffsets[i] : arOffsets[i] - arOffsets[i - 1];
            Console.WriteLine("track " + i + " length(s) " + length / 44100.0);
            WaveSegment seg = new WaveSegment();
            seg.m_writer     = new CWaveWrite(sOut, i, length);
            seg.m_nStopPoint = arOffsets[i];
            arSegments.Add(seg);
        }

        // stream through the audio
        CCutWaveProcessor processor = new CCutWaveProcessor(arSegments);

        CWavStreamReader.StreamThroughWaveFile(sWavInput, processor);
        if (nSamples != processor.GetNumberSamplesSeen())
        {
            Console.WriteLine("Warning, processor did not see all samples.");
        }

        // save files and close file handles.
        for (int i = 0; i < arSegments.Count; i++)
        {
            arSegments[i].m_writer.Save();
        }
    }