/// Version 1.0.0 /// Last modified 24.5.2014 /// /// Copyright (C) 2014 Veli-Mikko Puupponen /// /// Halyri-system is a prototype emergency call system. Its purpose is to /// demonstrate the use of the advanced capabilities available in the current /// generation smartphones in facilitating the emergency service dispatcher's /// capability to determine the nature of the emergency and to dispatch help. /// /// For more information, see the README file of this package. /// /// The MIT License (MIT) /// /// Permission is hereby granted, free of charge, to any person obtaining a copy /// of this software and associated documentation files (the "Software"), to /// deal in the Software without restriction, including without limitation the /// rights to use, copy, modify, merge, publish, distribute, sublicense, /// and/or sell copies of the Software, and to permit persons to whom the /// Software is furnished to do so, subject to the following conditions: /// /// The above copyright notice and this permission notice shall be included in /// all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING /// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS /// IN THE SOFTWARE. /// using System; using NSpeex; namespace Hake_WPF.AudioVideoManagers { /// Veli-Mikko Puupponen /// /// The class provides static methods for compression and decompression of /// speex encoded audio segments. /// class SpeexCompression { /// /// The function decompresses the provided MediaPackets of payload data using the /// provided SpeexDecoder.It returns the resulting PCM samples in a byte array. /// /// The SpeexDecodes is assumed to operate in the BandMode.Wide and the /// encoded data to conform to this format. /// /// If the decompression fails, it returns an empty array. /// /// SpeexDecodes instance used to decode the speex encoding. /// Speex encoded audio. /// The count of 16 bit PCM samples that were /// encoded to produce the provided encoded data. /// The resulting PCM samples or an empty array. public static byte[] DecompressSpeex(SpeexDecoder decoder, byte[] data, int originatingLength) { if (decoder == null || data == null) return new byte[0]; short[] decodedData = new short[originatingLength]; try { decoder.Decode(data, 0, data.Length, decodedData, 0, false); } catch (Exception) //TODO could be more specific, would need checking NSpeex code { return new byte[0]; } byte[] decodedBytes = new byte[decodedData.Length * 2]; int byteIndex = 0; for (int i = 0; i < decodedData.Length; i++, byteIndex += 2) { byte[] bytes = BitConverter.GetBytes(decodedData[i]); decodedBytes[byteIndex] = bytes[0]; decodedBytes[byteIndex + 1] = bytes[1]; } return decodedBytes; } /// /// The function encodes the provided PCM samples using the provided SpeexEncoder. /// The resulting encoded bytes are put into the compressed array. /// It returns the count of 16 bit samples that was used as the input of the /// speex encoder. /// /// If the count of the PCM samples is not a multiple of 320, the differece /// is padded with silence after the samples. /// /// The SpeexEncoder is assumed to be operating in the BandMode.Wide and /// sampleSizeBytes is assumed to be 2, i.e. 16bit PCM. /// /// 16Bit PCM samples to be encode into speex /// The size of the PCM samples in bytes, it should be 2. /// SpeexEncoder used to encode the data /// The target array for the resulting speex encoded audio. /// The count of 16Bit PCM samples compressed. public static int CompressSpeex(byte[] pcmSegment, int pcmByteCount, int sampleSizeBytes, SpeexEncoder encoder, out byte[] compressed) { compressed = new byte[0]; if (pcmSegment == null || pcmByteCount == 0 || encoder == null) return 0; int audioDataLength = pcmByteCount / sampleSizeBytes; int remainsFromCompletePackets = audioDataLength % encoder.FrameSize; short[] compressibleData; if (remainsFromCompletePackets == 0) compressibleData = new short[audioDataLength]; else compressibleData = new short[(audioDataLength - remainsFromCompletePackets) + encoder.FrameSize]; int compressibleDataIndex = 0; for (int i = 0; i < pcmByteCount; i += 2, compressibleDataIndex++) compressibleData[compressibleDataIndex] = BitConverter.ToInt16(pcmSegment, i); byte[] compressedData = new byte[pcmByteCount]; int actualCompressedByteCount = 0; try { actualCompressedByteCount = encoder.Encode(compressibleData, 0, compressibleData.Length, compressedData, 0, compressibleData.Length); } catch (Exception) //TODO could be more specific, would need checking NSpeex code { return 0; } if (actualCompressedByteCount > 0) { compressed = new byte[actualCompressedByteCount]; Array.Copy(compressedData, 0, compressed, 0, compressed.Length); } return compressibleData.Length; } } }