This class, borrowed almost unchanged from HearThis, compresses .wav files to .mp3. Requires the (currently separate) installation of LAME.
Пример #1
0
        private void Recorder_Stopped(IAudioRecorder arg1, ErrorEventArgs arg2)
        {
            Recorder.Stopped -= Recorder_Stopped;
            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(PathToCurrentAudioSegment));             // make sure audio directory exists
            int millisecondsToTrimFromEndForMouseClick = 100;

            try
            {
                var minimum = TimeSpan.FromMilliseconds(300);           // this is arbitrary
                AudioRecorder.TrimWavFile(PathToTemporaryWav, PathToCurrentAudioSegment, new TimeSpan(), TimeSpan.FromMilliseconds(millisecondsToTrimFromEndForMouseClick), minimum);
                RobustFile.Delete(PathToTemporaryWav);                  // Otherwise, these continue to clutter up the temp directory.
            }
            catch (Exception error)
            {
                Logger.WriteEvent(error.Message);
                RobustFile.Copy(PathToTemporaryWav, PathToCurrentAudioSegment, true);
            }

            //We don't actually need the mp3 now, so let people play with recording even without LAME (previously it could crash BL-3159).
            //We could put this off entirely until we make the ePUB.
            //I'm just gating this for now because maybe the thought was that it's better to do it a little at a time?
            //That's fine so long as it doesn't make the UI unresponsive on slow machines.
            if (LameEncoder.IsAvailable())
            {
                _mp3Encoder.Encode(PathToCurrentAudioSegment, PathToCurrentAudioSegment.Substring(0, PathToCurrentAudioSegment.Length - 4), new NullProgress());
                // Note: we need to keep the .wav file as well as the mp3 one. The mp3 format (or alternative mp4)
                // is required for ePUB. The wav file is a better permanent record of the recording; also,
                // it is used for playback.
            }
        }
Пример #2
0
 /// <summary>
 /// Make a compressed audio file for the specified .wav file.
 /// (Or return null if it can't be done becaus we don't have a LAME package installed.)
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 // internal and virtual for testing.
 internal virtual string MakeCompressedAudio(string wavPath)
 {
     // We have a recording, but not compressed. Possibly the LAME package was installed after
     // the recordings were made. Compress it now.
     if (_mp3Encoder == null)
     {
         if (!LameEncoder.IsAvailable())
         {
             return null;
         }
         _mp3Encoder = new LameEncoder();
     }
     _mp3Encoder.Encode(wavPath, wavPath.Substring(0, wavPath.Length - 4), new NullProgress());
     return Path.ChangeExtension(wavPath, "mp3");
 }