├── .travis.yml ├── LICENSE ├── README.md ├── audio.go ├── conv.go ├── conv_test.go ├── doc.go ├── float_buffer.go ├── float_buffer_test.go ├── formats.go ├── go.mod ├── int_buffer.go ├── int_buffer_test.go └── pcm_buffer.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.7.6 4 | - 1.8.x 5 | - 1.9.x 6 | - 1.10.x 7 | 8 | sudo: false 9 | 10 | before_install: 11 | - go get -t -v ./... 12 | 13 | script: 14 | - go test -race -coverprofile=coverage.txt -covermode=atomic 15 | 16 | after_success: 17 | - bash <(curl -s https://codecov.io/bash) 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2016 Matt Aimonetti 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # audio 2 | 3 | [![GoDoc](http://godoc.org/github.com/go-audio/audio?status.svg)](http://godoc.org/github.com/go-audio/audio) 4 | 5 | `audio` is a generic Go package designed to define a common interface to analyze 6 | and/or process audio data. 7 | 8 | At the heart of the package is the `Buffer` interface and its implementations: 9 | 10 | * `FloatBuffer` 11 | * `Float32Buffer` 12 | * `IntBuffer` 13 | 14 | Decoders, encoders, processors, analyzers and transformers can be written to 15 | accept or return these types and share a common interface. 16 | 17 | The idea is that audio libraries can define this interface or its 18 | implementations as input and return an `audio.Buffer` interface allowing all 19 | audio libraries to be chainable. 20 | 21 | ## Performance 22 | 23 | The buffer implementations are designed so a buffer can be reused and mutated 24 | avoiding allocation penalties. 25 | 26 | It is recommended to avoid using `Float32Buffer` unless performance is critical. 27 | The major drawback of using float32s is that the Go stdlib was designed to work 28 | with float64 and therefore the access to standard packages is limited. 29 | 30 | ## Usage 31 | 32 | Examples of how to use this interface is available under the 33 | [go-audio](https://github.com/go-audio) organization. 34 | -------------------------------------------------------------------------------- /audio.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | var ( 8 | // ErrInvalidBuffer is a generic error returned when trying to read/write to an invalid buffer. 9 | ErrInvalidBuffer = errors.New("invalid buffer") 10 | ) 11 | 12 | // Format is a high level representation of the underlying data. 13 | type Format struct { 14 | // NumChannels is the number of channels contained in the data 15 | NumChannels int 16 | // SampleRate is the sampling rate in Hz 17 | SampleRate int 18 | } 19 | 20 | // Buffer is the representation of an audio buffer. 21 | type Buffer interface { 22 | // PCMFormat is the format of buffer (describing the buffer content/format). 23 | PCMFormat() *Format 24 | // NumFrames returns the number of frames contained in the buffer. 25 | NumFrames() int 26 | // AsFloatBuffer returns a float 64 buffer from this buffer. 27 | AsFloatBuffer() *FloatBuffer 28 | // AsFloat32Buffer returns a float 32 buffer from this buffer. 29 | AsFloat32Buffer() *Float32Buffer 30 | // AsIntBuffer returns an int buffer from this buffer. 31 | AsIntBuffer() *IntBuffer 32 | // Clone creates a clean clone that can be modified without 33 | // changing the source buffer. 34 | Clone() Buffer 35 | } 36 | -------------------------------------------------------------------------------- /conv.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import "math" 4 | 5 | // IntMaxSignedValue returns the max value of an integer 6 | // based on its memory size 7 | func IntMaxSignedValue(b int) int { 8 | switch b { 9 | case 8: 10 | return 255 / 2 11 | case 16: 12 | return 65535 / 2 13 | case 24: 14 | return 16777215 / 2 15 | case 32: 16 | return 4294967295 / 2 17 | default: 18 | return 0 19 | } 20 | } 21 | 22 | // IEEEFloatToInt converts a 10 byte IEEE float into an int. 23 | func IEEEFloatToInt(b [10]byte) int { 24 | var i uint32 25 | // Negative number 26 | if (b[0] & 0x80) == 1 { 27 | return 0 28 | } 29 | 30 | // Less than 1 31 | if b[0] <= 0x3F { 32 | return 1 33 | } 34 | 35 | // Too big 36 | if b[0] > 0x40 { 37 | return 67108864 38 | } 39 | 40 | // Still too big 41 | if b[0] == 0x40 && b[1] > 0x1C { 42 | return 800000000 43 | } 44 | 45 | i = (uint32(b[2]) << 23) | (uint32(b[3]) << 15) | (uint32(b[4]) << 7) | (uint32(b[5]) >> 1) 46 | i >>= (29 - uint32(b[1])) 47 | 48 | return int(i) 49 | } 50 | 51 | // IntToIEEEFloat converts an int into a 10 byte IEEE float. 52 | func IntToIEEEFloat(i int) [10]byte { 53 | b := [10]byte{} 54 | num := float64(i) 55 | 56 | var sign int 57 | var expon int 58 | var fMant, fsMant float64 59 | var hiMant, loMant uint 60 | 61 | if num < 0 { 62 | sign = 0x8000 63 | } else { 64 | sign = 0 65 | } 66 | 67 | if num == 0 { 68 | expon = 0 69 | hiMant = 0 70 | loMant = 0 71 | } else { 72 | fMant, expon = math.Frexp(num) 73 | if (expon > 16384) || !(fMant < 1) { /* Infinity or NaN */ 74 | expon = sign | 0x7FFF 75 | hiMant = 0 76 | loMant = 0 /* infinity */ 77 | } else { /* Finite */ 78 | expon += 16382 79 | if expon < 0 { /* denormalized */ 80 | fMant = math.Ldexp(fMant, expon) 81 | expon = 0 82 | } 83 | expon |= sign 84 | fMant = math.Ldexp(fMant, 32) 85 | fsMant = math.Floor(fMant) 86 | hiMant = uint(fsMant) 87 | fMant = math.Ldexp(fMant-fsMant, 32) 88 | fsMant = math.Floor(fMant) 89 | loMant = uint(fsMant) 90 | } 91 | } 92 | 93 | b[0] = byte(expon >> 8) 94 | b[1] = byte(expon) 95 | b[2] = byte(hiMant >> 24) 96 | b[3] = byte(hiMant >> 16) 97 | b[4] = byte(hiMant >> 8) 98 | b[5] = byte(hiMant) 99 | b[6] = byte(loMant >> 24) 100 | b[7] = byte(loMant >> 16) 101 | b[8] = byte(loMant >> 8) 102 | b[9] = byte(loMant) 103 | 104 | return b 105 | } 106 | 107 | // Uint24to32 converts a 3 byte uint23 into a uint32 108 | // BigEndian! 109 | func Uint24to32(bytes []byte) uint32 { 110 | var output uint32 111 | output |= uint32(bytes[2]) << 0 112 | output |= uint32(bytes[1]) << 8 113 | output |= uint32(bytes[0]) << 16 114 | 115 | return output 116 | } 117 | 118 | // Int24BETo32 converts an int24 value from 3 bytes into an int32 value 119 | func Int24BETo32(bytes []byte) int32 { 120 | if len(bytes) < 3 { 121 | return 0 122 | } 123 | ss := int32(0xFF&bytes[0])<<16 | int32(0xFF&bytes[1])<<8 | int32(0xFF&bytes[2]) 124 | if (ss & 0x800000) > 0 { 125 | ss |= ^0xffffff 126 | } 127 | 128 | return ss 129 | } 130 | 131 | // Int24LETo32 converts an int24 value from 3 bytes into an int32 value 132 | func Int24LETo32(bytes []byte) int32 { 133 | if len(bytes) < 3 { 134 | return 0 135 | } 136 | ss := int32(bytes[0]) | int32(bytes[1])<<8 | int32(bytes[2])<<16 137 | if (ss & 0x800000) > 0 { 138 | ss |= ^0xffffff 139 | } 140 | 141 | return ss 142 | } 143 | 144 | // Uint32toUint24Bytes converts a uint32 into a 3 byte uint24 representation 145 | func Uint32toUint24Bytes(n uint32) []byte { 146 | bytes := make([]byte, 3) 147 | bytes[0] = byte(n >> 16) 148 | bytes[1] = byte(n >> 8) 149 | bytes[2] = byte(n >> 0) 150 | 151 | return bytes 152 | } 153 | 154 | // Int32toInt24LEBytes converts an int32 into a little endian 3 byte int24 representation 155 | func Int32toInt24LEBytes(n int32) []byte { 156 | bytes := make([]byte, 3) 157 | if (n & 0x800000) > 0 { 158 | n |= ^0xffffff 159 | } 160 | bytes[2] = byte(n >> 16) 161 | bytes[1] = byte(n >> 8) 162 | bytes[0] = byte(n >> 0) 163 | return bytes 164 | } 165 | 166 | // Int32toInt24BEBytes converts an int32 into a big endian 3 byte int24 representation 167 | func Int32toInt24BEBytes(n int32) []byte { 168 | bytes := make([]byte, 3) 169 | if (n & 0x800000) > 0 { 170 | n |= ^0xffffff 171 | } 172 | bytes[0] = byte(n >> 16) 173 | bytes[1] = byte(n >> 8) 174 | bytes[2] = byte(n >> 0) 175 | 176 | return bytes 177 | } 178 | -------------------------------------------------------------------------------- /conv_test.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import ( 4 | "bytes" 5 | "math" 6 | "testing" 7 | ) 8 | 9 | func TestInt24BETo32(t *testing.T) { 10 | tests := []struct { 11 | name string 12 | bytes []byte 13 | want int32 14 | }{ 15 | {"max", []byte{0x7F, 0xFF, 0xFF}, 8388607}, 16 | {"mid", []byte{0xFF, 0xFF, 0xFF}, -1}, 17 | {"min", []byte{0x80, 0x00, 0x01}, -8388607}, 18 | {"random", []byte{0x5D, 0xCB, 0xED}, 6147053}, 19 | {"random inverted", []byte{0xA2, 0x34, 0x13}, -6147053}, 20 | } 21 | for _, tt := range tests { 22 | t.Run(tt.name, func(t *testing.T) { 23 | if got := Int24BETo32(tt.bytes); got != tt.want { 24 | t.Errorf("Int24BETo32() = %v, want %v", got, tt.want) 25 | } 26 | }) 27 | } 28 | } 29 | 30 | func TestInt24LETo32(t *testing.T) { 31 | tests := []struct { 32 | name string 33 | bytes []byte 34 | want int32 35 | }{ 36 | {"max", []byte{0xFF, 0xFF, 0x7F}, 8388607}, 37 | {"mid", []byte{0xFF, 0xFF, 0xFF}, -1}, 38 | {"min", []byte{0x01, 0x00, 0x80}, -8388607}, 39 | {"random", []byte{0xED, 0xCB, 0x5D}, 6147053}, 40 | {"random inverted", []byte{0x13, 0x34, 0xA2}, -6147053}, 41 | } 42 | for _, tt := range tests { 43 | t.Run(tt.name, func(t *testing.T) { 44 | if got := Int24LETo32(tt.bytes); got != tt.want { 45 | t.Errorf("Int24LETo32() = %v, want %v", got, tt.want) 46 | } 47 | }) 48 | } 49 | } 50 | 51 | func TestInt32toInt24BEBytes(t *testing.T) { 52 | tests := []struct { 53 | name string 54 | want []byte 55 | val int32 56 | }{ 57 | {name: "mid", want: []byte{0xFF, 0xFF, 0xFF}, val: -1}, 58 | {name: "max", want: []byte{0x7F, 0xFF, 0xFF}, val: 8388607}, 59 | {name: "min", want: []byte{0x80, 0x00, 0x01}, val: -8388607}, 60 | {name: "random", want: []byte{0x5D, 0xCB, 0xED}, val: 6147053}, 61 | {name: "random inverted", want: []byte{0xA2, 0x34, 0x13}, val: -6147053}, 62 | } 63 | for _, tt := range tests { 64 | t.Run(tt.name, func(t *testing.T) { 65 | if got := Int32toInt24BEBytes(tt.val); bytes.Compare(tt.want, got) != 0 { 66 | t.Errorf("Int32toInt24BEBytes(%d) = %x, want %x", tt.val, got, tt.want) 67 | } 68 | }) 69 | } 70 | } 71 | 72 | func TestInt32toInt24LEBytes(t *testing.T) { 73 | tests := []struct { 74 | name string 75 | want []byte 76 | val int32 77 | }{ 78 | {name: "mid", want: []byte{0xFF, 0xFF, 0xFF}, val: -1}, 79 | {name: "max", want: []byte{0xFF, 0xFF, 0x7F}, val: 8388607}, 80 | {name: "min", want: []byte{0x01, 0x00, 0x80}, val: -8388607}, 81 | {name: "random", want: []byte{0xED, 0xCB, 0x5D}, val: 6147053}, 82 | {name: "random inverted", want: []byte{0x13, 0x34, 0xA2}, val: -6147053}, 83 | } 84 | for _, tt := range tests { 85 | t.Run(tt.name, func(t *testing.T) { 86 | if got := Int32toInt24LEBytes(tt.val); bytes.Compare(tt.want, got) != 0 { 87 | t.Errorf("Int32toInt24LEBytes(%d) = %x, want %x", tt.val, got, tt.want) 88 | } 89 | }) 90 | } 91 | } 92 | 93 | // round tripping similar to aiff to wav 94 | func TestInt24BETo32ToLEToInt32(t *testing.T) { 95 | tests := []struct { 96 | name string 97 | be []byte 98 | le []byte 99 | val int32 100 | }{ 101 | {name: "mid", be: []byte{0xFF, 0xFF, 0xFF}, le: []byte{0xff, 0xff, 0xff}, val: -1}, 102 | {name: "max", be: []byte{0x7F, 0xFF, 0xFF}, le: []byte{0xff, 0xff, 0x7f}, val: 8388607}, 103 | {name: "min", be: []byte{0x80, 0x00, 0x01}, le: []byte{0x01, 0x00, 0x80}, val: -8388607}, 104 | {name: "random", be: []byte{0x5D, 0xCB, 0xED}, le: []byte{0xED, 0xCB, 0x5D}, val: 6147053}, 105 | {name: "random inverted", be: []byte{0xA2, 0x34, 0x13}, le: []byte{0x13, 0x34, 0xA2}, val: -6147053}, 106 | } 107 | for _, tt := range tests { 108 | t.Run(tt.name, func(t *testing.T) { 109 | beV := Int24BETo32(tt.be) 110 | if beV != tt.val { 111 | t.Errorf("Int24BETo32(%x) = %d, want %d", tt.be, beV, tt.val) 112 | } 113 | leB := Int32toInt24LEBytes(beV) 114 | if bytes.Compare(leB, tt.le) != 0 { 115 | t.Errorf("Int32toInt24LEBytes(%d) = %#v, want %#v", beV, leB, tt.le) 116 | } 117 | leV := Int24LETo32(leB) 118 | if leV != tt.val { 119 | t.Errorf("Int24LETo32(%#v) = %d, want %d", leB, leV, tt.val) 120 | } 121 | }) 122 | } 123 | } 124 | 125 | func TestUint32toUint24Bytes(t *testing.T) { 126 | tests := []struct { 127 | name string 128 | be []byte 129 | val uint32 130 | }{ 131 | {name: "mid", be: []byte{0x0, 0x0, 0x1}, val: 1}, 132 | {name: "max", be: []byte{0xFF, 0xFF, 0xFF}, val: math.MaxUint32}, 133 | {name: "random", be: []byte{0x5D, 0xCB, 0xED}, val: 6147053}, 134 | } 135 | for _, tt := range tests { 136 | t.Run(tt.name, func(t *testing.T) { 137 | beB := Uint32toUint24Bytes(tt.val) 138 | if bytes.Compare(beB, tt.be) != 0 { 139 | t.Errorf("Uint32toUint24Bytes(%d) = %x, want %x", tt.val, beB, tt.be) 140 | } 141 | }) 142 | } 143 | } 144 | 145 | func TestUint24to32(t *testing.T) { 146 | tests := []struct { 147 | name string 148 | be []byte 149 | val uint32 150 | }{ 151 | {name: "mid", be: []byte{0x0, 0x0, 0x1}, val: 1}, 152 | {name: "max", be: []byte{0xff, 0xff, 0xff}, val: 16777215}, 153 | {name: "random", be: []byte{0x5D, 0xCB, 0xED}, val: 6147053}, 154 | } 155 | for _, tt := range tests { 156 | t.Run(tt.name, func(t *testing.T) { 157 | val := Uint24to32(tt.be) 158 | if val != tt.val { 159 | t.Errorf("Uint24to32(%x) = %d, want %d", tt.be, val, tt.val) 160 | } 161 | }) 162 | } 163 | } 164 | 165 | func TestIntToIEEEFloat(t *testing.T) { 166 | tests := []struct { 167 | name string 168 | ret [10]byte 169 | val int 170 | val2 int 171 | }{ 172 | {name: "min", ret: [10]byte{0x3f, 0xff, 0x80}, val: 1, val2: 1}, 173 | {name: "max", ret: [10]byte{0x40, 0x3e, 0x80}, val: math.MaxInt64, val2: 800000000}, // IEEEFloatToInt truncates 174 | {name: "random", ret: [10]byte{0x40, 0x15, 0xbb, 0x97, 0xda}, val: 6147053, val2: 6147053}, 175 | } 176 | for _, tt := range tests { 177 | t.Run(tt.name, func(t *testing.T) { 178 | val := IntToIEEEFloat(tt.val) 179 | if val != tt.ret { 180 | t.Errorf("IntToIEEEFloat(%d) = %x, want %x", tt.val, val, tt.ret) 181 | } 182 | }) 183 | } 184 | for _, tt := range tests { 185 | t.Run(tt.name, func(t *testing.T) { 186 | val := IEEEFloatToInt(tt.ret) 187 | if val != tt.val2 { 188 | t.Errorf("IEEEFloatToInt(%x) = %d, want %d", tt.ret, val, tt.val2) 189 | } 190 | }) 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package audio defines a common 3 | interface to analyze and/or process audio data. 4 | 5 | At the heart of the package is the Buffer interface and its implementations: 6 | FloatBuffer and IntBuffer. 7 | Decoders, encoders, processors, analyzers and transformers can be written to 8 | accept or return these types and share a common interface. 9 | */ 10 | package audio 11 | -------------------------------------------------------------------------------- /float_buffer.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | var _ Buffer = (*FloatBuffer)(nil) 4 | var _ Buffer = (*Float32Buffer)(nil) 5 | 6 | // FloatBuffer is an audio buffer with its PCM data formatted as float64. 7 | type FloatBuffer struct { 8 | // Format is the representation of the underlying data format 9 | Format *Format 10 | // Data is the buffer PCM data as floats 11 | Data []float64 12 | } 13 | 14 | // PCMFormat returns the buffer format information. 15 | func (buf *FloatBuffer) PCMFormat() *Format { return buf.Format } 16 | 17 | // AsFloatBuffer implements the Buffer interface and returns itself. 18 | func (buf *FloatBuffer) AsFloatBuffer() *FloatBuffer { return buf } 19 | 20 | // AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself. 21 | func (buf *FloatBuffer) AsFloat32Buffer() *Float32Buffer { 22 | newB := &Float32Buffer{} 23 | newB.Data = make([]float32, len(buf.Data)) 24 | for i := 0; i < len(buf.Data); i++ { 25 | newB.Data[i] = float32(buf.Data[i]) 26 | } 27 | newB.Format = &Format{ 28 | NumChannels: buf.Format.NumChannels, 29 | SampleRate: buf.Format.SampleRate, 30 | } 31 | return newB 32 | } 33 | 34 | // AsIntBuffer returns a copy of this buffer but with data truncated to Ints. 35 | func (buf *FloatBuffer) AsIntBuffer() *IntBuffer { 36 | newB := &IntBuffer{} 37 | newB.Data = make([]int, len(buf.Data)) 38 | for i := 0; i < len(buf.Data); i++ { 39 | newB.Data[i] = int(buf.Data[i]) 40 | } 41 | newB.Format = &Format{ 42 | NumChannels: buf.Format.NumChannels, 43 | SampleRate: buf.Format.SampleRate, 44 | } 45 | return newB 46 | } 47 | 48 | // Clone creates a clean clone that can be modified without 49 | // changing the source buffer. 50 | func (buf *FloatBuffer) Clone() Buffer { 51 | if buf == nil { 52 | return nil 53 | } 54 | newB := &FloatBuffer{} 55 | newB.Data = make([]float64, len(buf.Data)) 56 | copy(newB.Data, buf.Data) 57 | newB.Format = &Format{ 58 | NumChannels: buf.Format.NumChannels, 59 | SampleRate: buf.Format.SampleRate, 60 | } 61 | return newB 62 | } 63 | 64 | // NumFrames returns the number of frames contained in the buffer. 65 | func (buf *FloatBuffer) NumFrames() int { 66 | if buf == nil || buf.Format == nil { 67 | return 0 68 | } 69 | numChannels := buf.Format.NumChannels 70 | if numChannels == 0 { 71 | numChannels = 1 72 | } 73 | 74 | return len(buf.Data) / numChannels 75 | } 76 | 77 | // Float32Buffer is an audio buffer with its PCM data formatted as float32. 78 | type Float32Buffer struct { 79 | // Format is the representation of the underlying data format 80 | Format *Format 81 | // Data is the buffer PCM data as floats 82 | Data []float32 83 | // SourceBitDepth helps us know if the source was encoded on 84 | // 8, 16, 24, 32, 64 bits. 85 | SourceBitDepth int 86 | } 87 | 88 | // PCMFormat returns the buffer format information. 89 | func (buf *Float32Buffer) PCMFormat() *Format { return buf.Format } 90 | 91 | // AsFloatBuffer implements the Buffer interface and returns a float64 version of itself. 92 | func (buf *Float32Buffer) AsFloatBuffer() *FloatBuffer { 93 | newB := &FloatBuffer{} 94 | newB.Data = make([]float64, len(buf.Data)) 95 | for i := 0; i < len(buf.Data); i++ { 96 | newB.Data[i] = float64(buf.Data[i]) 97 | } 98 | newB.Format = &Format{ 99 | NumChannels: buf.Format.NumChannels, 100 | SampleRate: buf.Format.SampleRate, 101 | } 102 | return newB 103 | } 104 | 105 | // AsFloat32Buffer implements the Buffer interface and returns itself. 106 | func (buf *Float32Buffer) AsFloat32Buffer() *Float32Buffer { return buf } 107 | 108 | // AsIntBuffer returns a copy of this buffer but with data truncated to Ints. 109 | // It is usually recommended to apply a transforms when going from a 24bit source 110 | // to an int (16bit destination). Look at transforms.PCMScaleF32() for instance 111 | func (buf *Float32Buffer) AsIntBuffer() *IntBuffer { 112 | newB := &IntBuffer{SourceBitDepth: buf.SourceBitDepth} 113 | if newB.SourceBitDepth == 0 { 114 | newB.SourceBitDepth = 16 115 | } 116 | newB.Data = make([]int, len(buf.Data)) 117 | // TODO: we might want to consider checking the min/max values 118 | // and if we are in a normalized float range, apply a denormalization. 119 | for i := 0; i < len(buf.Data); i++ { 120 | newB.Data[i] = int(buf.Data[i]) 121 | } 122 | newB.Format = &Format{ 123 | NumChannels: buf.Format.NumChannels, 124 | SampleRate: buf.Format.SampleRate, 125 | } 126 | return newB 127 | } 128 | 129 | // Clone creates a clean clone that can be modified without 130 | // changing the source buffer. 131 | func (buf *Float32Buffer) Clone() Buffer { 132 | if buf == nil { 133 | return nil 134 | } 135 | newB := &Float32Buffer{} 136 | newB.Data = make([]float32, len(buf.Data)) 137 | copy(newB.Data, buf.Data) 138 | newB.Format = &Format{ 139 | NumChannels: buf.Format.NumChannels, 140 | SampleRate: buf.Format.SampleRate, 141 | } 142 | return newB 143 | } 144 | 145 | // NumFrames returns the number of frames contained in the buffer. 146 | func (buf *Float32Buffer) NumFrames() int { 147 | if buf == nil || buf.Format == nil { 148 | return 0 149 | } 150 | numChannels := buf.Format.NumChannels 151 | if numChannels == 0 { 152 | numChannels = 1 153 | } 154 | 155 | return len(buf.Data) / numChannels 156 | } 157 | -------------------------------------------------------------------------------- /float_buffer_test.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import ( 4 | "math" 5 | "reflect" 6 | "testing" 7 | ) 8 | 9 | func TestFloat64Buffer(t *testing.T) { 10 | tests := []struct { 11 | name string 12 | f64 []float64 13 | f32 []float32 14 | integer []int 15 | }{ 16 | {"float 64 conversion", []float64{1, 2, 3}, []float32{1, 2, 3}, []int{1, 2, 3}}, 17 | {"float 64 conversion, max float32", []float64{1, 2, float64(math.MaxFloat32)}, []float32{1, 2, math.MaxFloat32}, []int{1, 2, int(math.Inf(1))}}, 18 | {"float 64 conversion, inf", []float64{1, 2, math.MaxFloat64}, []float32{1, 2, float32(math.Inf(1))}, []int{1, 2, int(math.Inf(1))}}, 19 | } 20 | for _, tt := range tests { 21 | t.Run(tt.name, func(t *testing.T) { 22 | fb := FloatBuffer{Format: FormatMono22500, Data: tt.f64} 23 | fb32 := fb.AsFloat32Buffer() 24 | if !reflect.DeepEqual(fb32.Data, tt.f32) { 25 | t.Errorf("Expected %+v got %+v", tt.f32, fb32.Data) 26 | } 27 | integer := fb.AsIntBuffer() 28 | if !reflect.DeepEqual(integer.Data, tt.integer) { 29 | t.Errorf("Expected %+v got %+v", tt.integer, integer.Data) 30 | } 31 | }) 32 | } 33 | } 34 | 35 | func TestClone(t *testing.T) { 36 | tests := []struct { 37 | name string 38 | f64 []float64 39 | f32 []float32 40 | integer []int 41 | }{ 42 | {"float 64 conversion", []float64{1, 2, 3}, []float32{1, 2, 3}, []int{1, 2, 3}}, 43 | {"float 64 conversion, max float32", []float64{1, 2, float64(math.MaxFloat32)}, []float32{1, 2, math.MaxFloat32}, []int{1, 2, int(math.Inf(1))}}, 44 | {"float 64 conversion, inf", []float64{1, 2, math.MaxFloat64}, []float32{1, 2, float32(math.Inf(1))}, []int{1, 2, int(math.Inf(1))}}, 45 | } 46 | for _, tt := range tests { 47 | t.Run(tt.name, func(t *testing.T) { 48 | fb := FloatBuffer{Format: FormatMono22500, Data: tt.f64} 49 | b := fb.Clone() 50 | fb2 := b.AsFloatBuffer() 51 | if !reflect.DeepEqual(fb2.Data, tt.f64) { 52 | t.Errorf("Expected %+v got %+v", tt.f64, fb2.Data) 53 | } 54 | fb3 := b.AsFloat32Buffer() 55 | b = fb3.Clone() 56 | if !reflect.DeepEqual(fb3.Data, tt.f32) { 57 | t.Errorf("Expected %+v got %+v", tt.f32, fb3.Data) 58 | } 59 | fb4 := b.AsIntBuffer() 60 | b = fb4.Clone() 61 | if !reflect.DeepEqual(fb4.Data, tt.integer) { 62 | t.Errorf("Expected %+v got %+v", tt.integer, fb4.Data) 63 | } 64 | }) 65 | } 66 | } 67 | 68 | func TestNumFrames(t *testing.T) { 69 | expect := 3 70 | fb64 := FloatBuffer{Format: FormatMono22500, Data: []float64{1, 2, 3}} 71 | numFrames := fb64.NumFrames() 72 | if numFrames != expect { 73 | t.Errorf("Expected %d got %d", expect, numFrames) 74 | } 75 | fb32 := Float32Buffer{Format: FormatMono22500, Data: []float32{1, 2, 3}} 76 | numFrames = fb32.NumFrames() 77 | if numFrames != expect { 78 | t.Errorf("Expected %d got %d", expect, numFrames) 79 | } 80 | } 81 | 82 | func TestFloat32Buffer(t *testing.T) { 83 | tests := []struct { 84 | name string 85 | f64 []float64 86 | f32 []float32 87 | integer []int 88 | }{ 89 | {"float 32 conversion", []float64{1, 2, 3}, []float32{1, 2, 3}, []int{1, 2, 3}}, 90 | {"float 32 conversion, max float32", []float64{1, 2, float64(math.MaxFloat32)}, []float32{1, 2, math.MaxFloat32}, []int{1, 2, int(math.Inf(1))}}, 91 | {"float 32 conversion, inf", []float64{1, 2, math.Inf(1)}, []float32{1, 2, float32(math.Inf(1))}, []int{1, 2, int(math.Inf(1))}}, 92 | } 93 | for _, tt := range tests { 94 | t.Run(tt.name, func(t *testing.T) { 95 | fb := Float32Buffer{Format: FormatMono22500, Data: tt.f32} 96 | fb64 := fb.AsFloatBuffer() 97 | if !reflect.DeepEqual(fb64.Data, tt.f64) { 98 | t.Errorf("Expected %+v got %+v", tt.f64, fb64.Data) 99 | } 100 | integer := fb.AsIntBuffer() 101 | if !reflect.DeepEqual(integer.Data, tt.integer) { 102 | t.Errorf("Expected %+v got %+v", tt.integer, integer.Data) 103 | } 104 | }) 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /formats.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | var ( 4 | // MONO 5 | 6 | // FormatMono22500 is mono 22.5kHz format. 7 | FormatMono22500 = &Format{ 8 | NumChannels: 1, 9 | SampleRate: 22500, 10 | } 11 | // FormatMono44100 is mono 8bit 44.1kHz format. 12 | FormatMono44100 = &Format{ 13 | NumChannels: 1, 14 | SampleRate: 44100, 15 | } 16 | // FormatMono48000 is mono 48kHz format. 17 | FormatMono48000 = &Format{ 18 | NumChannels: 1, 19 | SampleRate: 48000, 20 | } 21 | // FormatMono96000 is mono 96kHz format. 22 | FormatMono96000 = &Format{ 23 | NumChannels: 1, 24 | SampleRate: 96000, 25 | } 26 | 27 | // STEREO 28 | 29 | // FormatStereo22500 is stereo 22.5kHz format. 30 | FormatStereo22500 = &Format{ 31 | NumChannels: 2, 32 | SampleRate: 22500, 33 | } 34 | // FormatStereo44100 is stereo 8bit 44.1kHz format. 35 | FormatStereo44100 = &Format{ 36 | NumChannels: 2, 37 | SampleRate: 44100, 38 | } 39 | // FormatStereo48000 is stereo 48kHz format. 40 | FormatStereo48000 = &Format{ 41 | NumChannels: 2, 42 | SampleRate: 48000, 43 | } 44 | // FormatStereo96000 is stereo 96kHz format. 45 | FormatStereo96000 = &Format{ 46 | NumChannels: 2, 47 | SampleRate: 96000, 48 | } 49 | ) 50 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/go-audio/audio 2 | -------------------------------------------------------------------------------- /int_buffer.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import "math" 4 | 5 | var _ Buffer = (*IntBuffer)(nil) 6 | 7 | // IntBuffer is an audio buffer with its PCM data formatted as int. 8 | type IntBuffer struct { 9 | // Format is the representation of the underlying data format 10 | Format *Format 11 | // Data is the buffer PCM data as ints 12 | Data []int 13 | // SourceBitDepth helps us know if the source was encoded on 14 | // 8, 16, 24, 32, 64 bits. 15 | SourceBitDepth int 16 | } 17 | 18 | // PCMFormat returns the buffer format information. 19 | func (buf *IntBuffer) PCMFormat() *Format { return buf.Format } 20 | 21 | // AsFloatBuffer returns a copy of this buffer but with data converted to floats. 22 | func (buf *IntBuffer) AsFloatBuffer() *FloatBuffer { 23 | newB := &FloatBuffer{} 24 | newB.Data = make([]float64, len(buf.Data)) 25 | for i := 0; i < len(buf.Data); i++ { 26 | newB.Data[i] = float64(buf.Data[i]) 27 | } 28 | newB.Format = &Format{ 29 | NumChannels: buf.Format.NumChannels, 30 | SampleRate: buf.Format.SampleRate, 31 | } 32 | return newB 33 | } 34 | 35 | // AsFloat32Buffer returns a copy of this buffer but with data converted to float 32. 36 | func (buf *IntBuffer) AsFloat32Buffer() *Float32Buffer { 37 | newB := &Float32Buffer{} 38 | newB.Data = make([]float32, len(buf.Data)) 39 | max := int64(0) 40 | // try to guess the bit depths without knowing the source 41 | if buf.SourceBitDepth == 0 { 42 | for _, s := range buf.Data { 43 | if int64(s) > max { 44 | max = int64(s) 45 | } 46 | } 47 | buf.SourceBitDepth = 8 48 | if max > 127 { 49 | buf.SourceBitDepth = 16 50 | } 51 | // greater than int16, expecting int24 52 | if max > 32767 { 53 | buf.SourceBitDepth = 24 54 | } 55 | // int 32 56 | if max > 8388607 { 57 | buf.SourceBitDepth = 32 58 | } 59 | // int 64 60 | if max > 4294967295 { 61 | buf.SourceBitDepth = 64 62 | } 63 | } 64 | newB.SourceBitDepth = buf.SourceBitDepth 65 | factor := math.Pow(2, float64(buf.SourceBitDepth)-1) 66 | for i := 0; i < len(buf.Data); i++ { 67 | newB.Data[i] = float32(float64(buf.Data[i]) / factor) 68 | } 69 | newB.Format = &Format{ 70 | NumChannels: buf.Format.NumChannels, 71 | SampleRate: buf.Format.SampleRate, 72 | } 73 | return newB 74 | } 75 | 76 | // AsIntBuffer implements the Buffer interface and returns itself. 77 | func (buf *IntBuffer) AsIntBuffer() *IntBuffer { return buf } 78 | 79 | // NumFrames returns the number of frames contained in the buffer. 80 | func (buf *IntBuffer) NumFrames() int { 81 | if buf == nil || buf.Format == nil { 82 | return 0 83 | } 84 | numChannels := buf.Format.NumChannels 85 | if numChannels == 0 { 86 | numChannels = 1 87 | } 88 | 89 | return len(buf.Data) / numChannels 90 | } 91 | 92 | // Clone creates a clean clone that can be modified without 93 | // changing the source buffer. 94 | func (buf *IntBuffer) Clone() Buffer { 95 | if buf == nil { 96 | return nil 97 | } 98 | newB := &IntBuffer{} 99 | newB.Data = make([]int, len(buf.Data)) 100 | copy(newB.Data, buf.Data) 101 | newB.Format = &Format{ 102 | NumChannels: buf.Format.NumChannels, 103 | SampleRate: buf.Format.SampleRate, 104 | } 105 | return newB 106 | } 107 | -------------------------------------------------------------------------------- /int_buffer_test.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestIntBuffer_AsFloat32Buffer(t *testing.T) { 8 | type fields struct { 9 | Range int 10 | SourceBitDepth int 11 | } 12 | tests := []struct { 13 | name string 14 | fields fields 15 | }{ 16 | {name: "16bit range", 17 | fields: fields{Range: int(int16(1<<15 - 1)), SourceBitDepth: 16}}, 18 | {name: "24bit range", 19 | fields: fields{Range: int(int32(1 << 23)), SourceBitDepth: 24}}, 20 | } 21 | for _, tt := range tests { 22 | t.Run(tt.name, func(t *testing.T) { 23 | buf := &IntBuffer{ 24 | Format: FormatMono44100, 25 | SourceBitDepth: tt.fields.SourceBitDepth, 26 | } 27 | intData := []int{ 28 | -tt.fields.Range, 29 | 0, 30 | tt.fields.Range, 31 | } 32 | buf.Data = intData 33 | got := buf.AsFloat32Buffer() 34 | for i, f := range got.Data { 35 | if f < -1.0 || f > 1.0 { 36 | t.Errorf("%d was converted out of range to %f", intData[i], f) 37 | } 38 | } 39 | }) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pcm_buffer.go: -------------------------------------------------------------------------------- 1 | package audio 2 | 3 | import "math" 4 | 5 | // PCMDataFormat is an enum type to indicate the underlying data format used. 6 | type PCMDataFormat uint8 7 | 8 | const ( 9 | // DataTypeUnknown refers to an unknown format 10 | DataTypeUnknown PCMDataFormat = iota 11 | // DataTypeI8 indicates that the content of the audio buffer made of 8-bit integers. 12 | DataTypeI8 13 | // DataTypeI16 indicates that the content of the audio buffer made of 16-bit integers. 14 | DataTypeI16 15 | // DataTypeI32 indicates that the content of the audio buffer made of 32-bit integers. 16 | DataTypeI32 17 | // DataTypeF32 indicates that the content of the audio buffer made of 32-bit floats. 18 | DataTypeF32 19 | // DataTypeF64 indicates that the content of the audio buffer made of 64-bit floats. 20 | DataTypeF64 21 | ) 22 | 23 | var _ Buffer = (*PCMBuffer)(nil) 24 | 25 | // PCMBuffer encapsulates uncompressed audio data 26 | // and provides useful methods to read/manipulate this PCM data. 27 | // It's a more flexible buffer type allowing the developer to handle 28 | // different kind of buffer data formats and convert between underlying 29 | // types. 30 | type PCMBuffer struct { 31 | // Format describes the format of the buffer data. 32 | Format *Format 33 | // I8 is a store for audio sample data as integers. 34 | I8 []int8 35 | // I16 is a store for audio sample data as integers. 36 | I16 []int16 37 | // I32 is a store for audio sample data as integers. 38 | I32 []int32 39 | // F32 is a store for audio samples data as float64. 40 | F32 []float32 41 | // F64 is a store for audio samples data as float64. 42 | F64 []float64 43 | // DataType indicates the primary format used for the underlying data. 44 | // The consumer of the buffer might want to look at this value to know what store 45 | // to use to optimaly retrieve data. 46 | DataType PCMDataFormat 47 | // SourceBitDepth helps us know if the source was encoded on 48 | // 1 (int8), 2 (int16), 3(int24), 4(int32), 8(int64) bytes. 49 | SourceBitDepth uint8 50 | } 51 | 52 | // Len returns the length of the underlying data. 53 | func (b *PCMBuffer) Len() int { 54 | if b == nil { 55 | return 0 56 | } 57 | 58 | switch b.DataType { 59 | case DataTypeI8: 60 | return len(b.I8) 61 | case DataTypeI16: 62 | return len(b.I16) 63 | case DataTypeI32: 64 | return len(b.I32) 65 | case DataTypeF32: 66 | return len(b.F32) 67 | case DataTypeF64: 68 | return len(b.F64) 69 | default: 70 | return 0 71 | } 72 | } 73 | 74 | // PCMFormat returns the buffer format information. 75 | func (b *PCMBuffer) PCMFormat() *Format { 76 | if b == nil { 77 | return nil 78 | } 79 | return b.Format 80 | } 81 | 82 | // NumFrames returns the number of frames contained in the buffer. 83 | func (b *PCMBuffer) NumFrames() int { 84 | if b == nil || b.Format == nil { 85 | return 0 86 | } 87 | numChannels := b.Format.NumChannels 88 | if numChannels == 0 { 89 | numChannels = 1 90 | } 91 | 92 | return b.Len() / numChannels 93 | } 94 | 95 | // AsFloatBuffer returns a copy of this buffer but with data converted to floats. 96 | func (b *PCMBuffer) AsFloatBuffer() *FloatBuffer { 97 | newB := &FloatBuffer{} 98 | newB.Data = b.AsF64() 99 | if b.Format != nil { 100 | newB.Format = &Format{ 101 | NumChannels: b.Format.NumChannels, 102 | SampleRate: b.Format.SampleRate, 103 | } 104 | } 105 | return newB 106 | } 107 | 108 | // AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself. 109 | func (b *PCMBuffer) AsFloat32Buffer() *Float32Buffer { 110 | newB := &Float32Buffer{} 111 | newB.Data = b.AsF32() 112 | if b.Format != nil { 113 | newB.Format = &Format{ 114 | NumChannels: b.Format.NumChannels, 115 | SampleRate: b.Format.SampleRate, 116 | } 117 | } 118 | return newB 119 | } 120 | 121 | // AsIntBuffer returns a copy of this buffer but with data truncated to Ints. 122 | func (b *PCMBuffer) AsIntBuffer() *IntBuffer { 123 | newB := &IntBuffer{} 124 | newB.Data = b.AsInt() 125 | if b.Format != nil { 126 | newB.Format = &Format{ 127 | NumChannels: b.Format.NumChannels, 128 | SampleRate: b.Format.SampleRate, 129 | } 130 | } 131 | return newB 132 | } 133 | 134 | // AsI8 returns the buffer's samples as int8 sample values. 135 | // If the buffer isn't in this format, a copy is created and converted. 136 | // Note that converting might result in loss of resolution. 137 | func (b *PCMBuffer) AsI8() (out []int8) { 138 | if b == nil { 139 | return nil 140 | } 141 | switch b.DataType { 142 | case DataTypeI8: 143 | return b.I8 144 | case DataTypeI16: 145 | out = make([]int8, len(b.I16)) 146 | for i := 0; i < len(b.I16); i++ { 147 | out[i] = int8(b.I16[i]) 148 | } 149 | case DataTypeI32: 150 | out = make([]int8, len(b.I32)) 151 | for i := 0; i < len(b.I32); i++ { 152 | out[i] = int8(b.I32[i]) 153 | } 154 | case DataTypeF32: 155 | out = make([]int8, len(b.F32)) 156 | for i := 0; i < len(b.F32); i++ { 157 | out[i] = int8(b.F32[i]) 158 | } 159 | case DataTypeF64: 160 | out = make([]int8, len(b.F64)) 161 | for i := 0; i < len(b.F64); i++ { 162 | out[i] = int8(b.F64[i]) 163 | } 164 | } 165 | return out 166 | } 167 | 168 | // AsI16 returns the buffer's samples as int16 sample values. 169 | // If the buffer isn't in this format, a copy is created and converted. 170 | // Note that converting might result in loss of resolution. 171 | func (b *PCMBuffer) AsI16() (out []int16) { 172 | if b == nil { 173 | return nil 174 | } 175 | switch b.DataType { 176 | case DataTypeI8: 177 | out = make([]int16, len(b.I8)) 178 | for i := 0; i < len(b.I8); i++ { 179 | out[i] = int16(b.I8[i]) 180 | } 181 | case DataTypeI16: 182 | return b.I16 183 | case DataTypeI32: 184 | out = make([]int16, len(b.I32)) 185 | for i := 0; i < len(b.I32); i++ { 186 | out[i] = int16(b.I32[i]) 187 | } 188 | case DataTypeF32: 189 | out = make([]int16, len(b.F32)) 190 | for i := 0; i < len(b.F32); i++ { 191 | out[i] = int16(b.F32[i]) 192 | } 193 | case DataTypeF64: 194 | out = make([]int16, len(b.F64)) 195 | for i := 0; i < len(b.F64); i++ { 196 | out[i] = int16(b.F64[i]) 197 | } 198 | } 199 | return out 200 | } 201 | 202 | // AsI32 returns the buffer's samples as int32 sample values. 203 | // If the buffer isn't in this format, a copy is created and converted. 204 | // Note that converting a float to an int might result in unexpected truncations. 205 | func (b *PCMBuffer) AsI32() (out []int32) { 206 | if b == nil { 207 | return nil 208 | } 209 | switch b.DataType { 210 | case DataTypeI8: 211 | out = make([]int32, len(b.I8)) 212 | for i := 0; i < len(b.I8); i++ { 213 | out[i] = int32(b.I8[i]) 214 | } 215 | case DataTypeI16: 216 | out = make([]int32, len(b.I16)) 217 | for i := 0; i < len(b.I16); i++ { 218 | out[i] = int32(b.I16[i]) 219 | } 220 | case DataTypeI32: 221 | return b.I32 222 | case DataTypeF32: 223 | out = make([]int32, len(b.F32)) 224 | for i := 0; i < len(b.F32); i++ { 225 | out[i] = int32(b.F32[i]) 226 | } 227 | case DataTypeF64: 228 | out = make([]int32, len(b.F64)) 229 | for i := 0; i < len(b.F64); i++ { 230 | out[i] = int32(b.F64[i]) 231 | } 232 | } 233 | return out 234 | } 235 | 236 | // AsInt returns the buffer content as integers (int32s). 237 | // It's recommended to avoid this method since it creates 238 | // an extra copy of the buffer content. 239 | func (b *PCMBuffer) AsInt() (out []int) { 240 | int32s := b.AsI32() 241 | out = make([]int, len(int32s)) 242 | for i := 0; i < len(int32s); i++ { 243 | out[i] = int(int32s[i]) 244 | } 245 | return out 246 | } 247 | 248 | // AsF32 returns the buffer's samples as float32 sample values. 249 | // If the buffer isn't in this format, a copy is created and converted. 250 | // Note that converting might result in unexpected truncations. 251 | func (b *PCMBuffer) AsF32() (out []float32) { 252 | if b == nil { 253 | return nil 254 | } 255 | switch b.DataType { 256 | case DataTypeI8: 257 | bitDepth := b.calculateIntBitDepth() 258 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 259 | out = make([]float32, len(b.I8)) 260 | for i := 0; i < len(b.I8); i++ { 261 | out[i] = float32(float64(int64(b.I8[i])) / factor) 262 | } 263 | case DataTypeI16: 264 | bitDepth := b.calculateIntBitDepth() 265 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 266 | out = make([]float32, len(b.I16)) 267 | for i := 0; i < len(b.I16); i++ { 268 | out[i] = float32(float64(int64(b.I16[i])) / factor) 269 | } 270 | case DataTypeI32: 271 | bitDepth := b.calculateIntBitDepth() 272 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 273 | out = make([]float32, len(b.I16)) 274 | for i := 0; i < len(b.I16); i++ { 275 | out[i] = float32(float64(int64(b.I16[i])) / factor) 276 | } 277 | case DataTypeF32: 278 | return b.F32 279 | case DataTypeF64: 280 | out = make([]float32, len(b.F64)) 281 | for i := 0; i < len(b.F64); i++ { 282 | out[i] = float32(b.F64[i]) 283 | } 284 | } 285 | return out 286 | } 287 | 288 | // AsF64 returns the buffer's samples as float64 sample values. 289 | // If the buffer isn't in this format, a copy is created and converted. 290 | // Note that converting might result in unexpected truncations. 291 | func (b *PCMBuffer) AsF64() (out []float64) { 292 | if b == nil { 293 | return nil 294 | } 295 | switch b.DataType { 296 | case DataTypeI8: 297 | bitDepth := b.calculateIntBitDepth() 298 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 299 | out = make([]float64, len(b.I8)) 300 | for i := 0; i < len(b.I8); i++ { 301 | out[i] = float64(int64(b.I8[i])) / factor 302 | } 303 | case DataTypeI16: 304 | bitDepth := b.calculateIntBitDepth() 305 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 306 | out = make([]float64, len(b.I16)) 307 | for i := 0; i < len(b.I16); i++ { 308 | out[i] = float64(int64(b.I16[i])) / factor 309 | } 310 | case DataTypeI32: 311 | bitDepth := b.calculateIntBitDepth() 312 | factor := math.Pow(2, 8*float64(bitDepth/8)-1) 313 | out = make([]float64, len(b.I16)) 314 | for i := 0; i < len(b.I16); i++ { 315 | out[i] = float64(int64(b.I16[i])) / factor 316 | } 317 | case DataTypeF32: 318 | out = make([]float64, len(b.F32)) 319 | for i := 0; i < len(b.F32); i++ { 320 | out[i] = float64(b.F32[i]) 321 | } 322 | case DataTypeF64: 323 | return b.F64 324 | } 325 | return out 326 | } 327 | 328 | // Clone creates a clean clone that can be modified without 329 | // changing the source buffer. 330 | func (b *PCMBuffer) Clone() Buffer { 331 | if b == nil { 332 | return nil 333 | } 334 | newB := &PCMBuffer{DataType: b.DataType} 335 | switch b.DataType { 336 | case DataTypeI8: 337 | newB.I8 = make([]int8, len(b.I8)) 338 | copy(newB.I8, b.I8) 339 | case DataTypeI16: 340 | newB.I16 = make([]int16, len(b.I16)) 341 | copy(newB.I16, b.I16) 342 | case DataTypeI32: 343 | newB.I32 = make([]int32, len(b.I32)) 344 | copy(newB.I32, b.I32) 345 | case DataTypeF32: 346 | newB.F32 = make([]float32, len(b.F32)) 347 | copy(newB.F32, b.F32) 348 | case DataTypeF64: 349 | newB.F64 = make([]float64, len(b.F64)) 350 | copy(newB.F64, b.F64) 351 | } 352 | 353 | newB.Format = &Format{ 354 | NumChannels: b.Format.NumChannels, 355 | SampleRate: b.Format.SampleRate, 356 | } 357 | return newB 358 | } 359 | 360 | // SwitchPrimaryType is a convenience method to switch the primary data type. 361 | // Use this if you process/swap a different type than the original type. 362 | // Notes that conversion might be lossy if you switch to a lower resolution format. 363 | func (b *PCMBuffer) SwitchPrimaryType(t PCMDataFormat) { 364 | if b == nil || t == b.DataType { 365 | return 366 | } 367 | switch t { 368 | case DataTypeI8: 369 | b.I8 = b.AsI8() 370 | b.I16 = nil 371 | b.I32 = nil 372 | b.F32 = nil 373 | b.F64 = nil 374 | case DataTypeI16: 375 | b.I8 = nil 376 | b.I16 = b.AsI16() 377 | b.I32 = nil 378 | b.F32 = nil 379 | b.F64 = nil 380 | case DataTypeI32: 381 | b.I8 = nil 382 | b.I16 = nil 383 | b.I32 = b.AsI32() 384 | b.F32 = nil 385 | b.F64 = nil 386 | case DataTypeF32: 387 | b.I8 = nil 388 | b.I16 = nil 389 | b.I32 = nil 390 | b.F32 = b.AsF32() 391 | b.F64 = nil 392 | case DataTypeF64: 393 | b.I8 = nil 394 | b.I16 = nil 395 | b.I32 = nil 396 | b.F32 = nil 397 | b.F64 = b.AsF64() 398 | } 399 | 400 | b.DataType = t 401 | } 402 | 403 | // calculateIntBithDepth looks at the int values in the buffer and returns 404 | // the required lowest bit depth. 405 | func (b *PCMBuffer) calculateIntBitDepth() uint8 { 406 | if b == nil { 407 | return 0 408 | } 409 | bitDepth := b.SourceBitDepth 410 | if bitDepth != 0 { 411 | return bitDepth 412 | } 413 | var max int64 414 | switch b.DataType { 415 | case DataTypeI8: 416 | var i8max int8 417 | for _, s := range b.I8 { 418 | if s > i8max { 419 | i8max = s 420 | } 421 | } 422 | max = int64(i8max) 423 | case DataTypeI16: 424 | var i16max int16 425 | for _, s := range b.I16 { 426 | if s > i16max { 427 | i16max = s 428 | } 429 | } 430 | max = int64(i16max) 431 | case DataTypeI32: 432 | var i32max int32 433 | for _, s := range b.I32 { 434 | if s > i32max { 435 | i32max = s 436 | } 437 | } 438 | max = int64(i32max) 439 | default: 440 | // This method is only meant to be used on int buffers. 441 | return bitDepth 442 | } 443 | bitDepth = 8 444 | if max > 127 { 445 | bitDepth = 16 446 | } 447 | // greater than int16, expecting int24 448 | if max > 32767 { 449 | bitDepth = 24 450 | } 451 | // int 32 452 | if max > 8388607 { 453 | bitDepth = 32 454 | } 455 | // int 64 456 | if max > 4294967295 { 457 | bitDepth = 64 458 | } 459 | 460 | return bitDepth 461 | } 462 | --------------------------------------------------------------------------------