|
| 1 | +/* This class has been written by |
| 2 | + * Corinna John (Hannover, Germany) |
| 3 | + |
| 4 | + * |
| 5 | + * You may do with this code whatever you like, |
| 6 | + * except selling it or claiming any rights/ownership. |
| 7 | + * |
| 8 | + * Please send me a little feedback about what you're |
| 9 | + * using this code for and what changes you'd like to |
| 10 | + * see in later versions. (And please excuse my bad english.) |
| 11 | + * |
| 12 | + * WARNING: This is experimental code. |
| 13 | + * Please do not expect "Release Quality". |
| 14 | + * */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.IO; |
| 18 | +using System.Runtime.InteropServices; |
| 19 | + |
| 20 | +namespace AviFile |
| 21 | +{ |
| 22 | + public class AudioStream : AviStream{ |
| 23 | + |
| 24 | + public int CountBitsPerSample{ |
| 25 | + get{ return waveFormat.wBitsPerSample; } |
| 26 | + } |
| 27 | + |
| 28 | + public int CountSamplesPerSecond{ |
| 29 | + get{ return waveFormat.nSamplesPerSec; } |
| 30 | + } |
| 31 | + |
| 32 | + public int CountChannels{ |
| 33 | + get{ return waveFormat.nChannels; } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary>the stream's format</summary> |
| 37 | + private Avi.PCMWAVEFORMAT waveFormat = new Avi.PCMWAVEFORMAT(); |
| 38 | + |
| 39 | + /// <summary>Initialize an AudioStream for an existing stream</summary> |
| 40 | + /// <param name="aviFile">The file that contains the stream</param> |
| 41 | + /// <param name="aviStream">An IAVISTREAM from [aviFile]</param> |
| 42 | + public AudioStream(int aviFile, IntPtr aviStream){ |
| 43 | + this.aviFile = aviFile; |
| 44 | + this.aviStream = aviStream; |
| 45 | + |
| 46 | + int size = Marshal.SizeOf(waveFormat); |
| 47 | + Avi.AVIStreamReadFormat(aviStream, 0, ref waveFormat, ref size); |
| 48 | + Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary>Read the stream's header information</summary> |
| 52 | + /// <param name="aviStream">The IAVISTREAM to read from</param> |
| 53 | + /// <returns>AVISTREAMINFO</returns> |
| 54 | + private Avi.AVISTREAMINFO GetStreamInfo(IntPtr aviStream){ |
| 55 | + Avi.AVISTREAMINFO streamInfo = new Avi.AVISTREAMINFO(); |
| 56 | + int result = Avi.AVIStreamInfo(aviStream, ref streamInfo, Marshal.SizeOf(streamInfo)); |
| 57 | + if(result != 0) { |
| 58 | + throw new Exception("Exception in AVIStreamInfo: "+result.ToString()); |
| 59 | + } |
| 60 | + return streamInfo; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary>Read the stream's header information</summary> |
| 64 | + /// <returns>AVISTREAMINFO</returns> |
| 65 | + public Avi.AVISTREAMINFO GetStreamInfo(){ |
| 66 | + if(writeCompressed){ |
| 67 | + return GetStreamInfo(compressedStream); |
| 68 | + }else{ |
| 69 | + return GetStreamInfo(aviStream); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary>Read the stream's format information</summary> |
| 74 | + /// <returns>PCMWAVEFORMAT</returns> |
| 75 | + public Avi.PCMWAVEFORMAT GetFormat(){ |
| 76 | + Avi.PCMWAVEFORMAT format = new Avi.PCMWAVEFORMAT(); |
| 77 | + int size = Marshal.SizeOf(format); |
| 78 | + int result = Avi.AVIStreamReadFormat(aviStream, 0, ref format, ref size); |
| 79 | + return format; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary>Returns all data needed to copy the stream</summary> |
| 83 | + /// <remarks>Do not forget to call Marshal.FreeHGlobal and release the raw data pointer</remarks> |
| 84 | + /// <param name="streamInfo">Receives the header information</param> |
| 85 | + /// <param name="format">Receives the format</param> |
| 86 | + /// <param name="streamLength">Receives the length of the stream</param> |
| 87 | + /// <returns>Pointer to the wave data</returns> |
| 88 | + public IntPtr GetStreamData(ref Avi.AVISTREAMINFO streamInfo, ref Avi.PCMWAVEFORMAT format, ref int streamLength){ |
| 89 | + streamInfo = GetStreamInfo(); |
| 90 | + |
| 91 | + format = GetFormat(); |
| 92 | + //length in bytes = length in samples * length of a sample |
| 93 | + streamLength = Avi.AVIStreamLength(aviStream.ToInt32()) * streamInfo.dwSampleSize; |
| 94 | + IntPtr waveData = Marshal.AllocHGlobal(streamLength); |
| 95 | + |
| 96 | + int result = Avi.AVIStreamRead(aviStream, 0, streamLength, waveData, streamLength, 0, 0); |
| 97 | + if(result != 0){ |
| 98 | + throw new Exception("Exception in AVIStreamRead: "+result.ToString()); |
| 99 | + } |
| 100 | + |
| 101 | + return waveData; |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary>Copy the stream into a new file</summary> |
| 105 | + /// <param name="fileName">Name of the new file</param> |
| 106 | + public override void ExportStream(String fileName){ |
| 107 | + Avi.AVICOMPRESSOPTIONS_CLASS opts = new Avi.AVICOMPRESSOPTIONS_CLASS(); |
| 108 | + opts.fccType = (UInt32)Avi.mmioStringToFOURCC("auds", 0); |
| 109 | + opts.fccHandler = (UInt32)Avi.mmioStringToFOURCC("CAUD", 0); |
| 110 | + opts.dwKeyFrameEvery = 0; |
| 111 | + opts.dwQuality = 0; |
| 112 | + opts.dwFlags = 0; |
| 113 | + opts.dwBytesPerSecond= 0; |
| 114 | + opts.lpFormat = new IntPtr(0); |
| 115 | + opts.cbFormat = 0; |
| 116 | + opts.lpParms = new IntPtr(0); |
| 117 | + opts.cbParms = 0; |
| 118 | + opts.dwInterleaveEvery = 0; |
| 119 | + |
| 120 | + Avi.AVISaveV(fileName, 0, 0, 1, ref aviStream, ref opts); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | +} |
0 commit comments