1 /* 2 Copyright (c) 2016-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Sample types and conversion utilities 31 * 32 * Copyright: Timur Gafarov 2016-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.audio.sample; 37 38 /** 39 * dlib.audio defines four integer sample formats: signed 8-bit, signed 16-bit, 40 * unsigned 8-bit, unsigned 16-bit. 41 * They are for storage only. All sound processing and sample I/O 42 * should be done in floating point numbers. Floating point sample is signed and 43 * ranges from -1.0f to 1.0f. 44 */ 45 enum SampleFormat 46 { 47 /// signed 8-bit 48 S8, 49 /// signed 16-bit 50 S16, 51 /// unsigned 8-bit 52 U8, 53 /// unsigned 16-bit 54 U16 55 } 56 57 /** 58 * Convert integer sample to floating point sample 59 */ 60 float toFloatSample(ubyte* ptr, SampleFormat format) 61 { 62 float res; 63 switch (format) 64 { 65 case SampleFormat.S8: 66 res = *cast(byte*)ptr; 67 res /= byte.max; 68 break; 69 case SampleFormat.S16: 70 res = *cast(short*)ptr; 71 res /= short.max; 72 break; 73 case SampleFormat.U8: 74 res = *ptr; 75 res /= ubyte.max; 76 res = res * 2.0f - 1.0f; // normalize to range -1..1 77 break; 78 case SampleFormat.U16: 79 res = *cast(ushort*)ptr; 80 res /= ushort.max; 81 res = res * 2.0f - 1.0f; // normalize to range -1..1 82 break; 83 default: 84 break; 85 } 86 return res; 87 } 88 89 /** 90 * Convert floating point sample to integer sample 91 */ 92 void fromFloatSample(ubyte* ptr, SampleFormat format, float s) 93 { 94 switch (format) 95 { 96 case SampleFormat.S8: 97 *cast(byte*)ptr = cast(byte)(s * byte.max); 98 break; 99 case SampleFormat.S16: 100 *cast(short*)ptr = cast(short)(s * short.max); 101 break; 102 case SampleFormat.U8: 103 *cast(ubyte*)ptr = cast(ubyte)((s + 1.0f) * 0.5f * ubyte.max); 104 break; 105 case SampleFormat.U16: 106 *cast(ushort*)ptr = cast(ushort)((s + 1.0f) * 0.5f * ushort.max); 107 break; 108 default: 109 break; 110 } 111 }