]> Sergey Matveev's repositories - vors.git/blob - cmd/client/audio.go
Noising
[vors.git] / cmd / client / audio.go
1 // VoRS -- Vo(IP) Really Simple
2 // Copyright (C) 2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "log"
20
21         vors "go.stargrave.org/vors/internal"
22         "gopkg.in/hraban/opus.v2"
23 )
24
25 func newOpusEnc() *opus.Encoder {
26         enc, err := opus.NewEncoder(vors.Rate, 1, opus.AppVoIP)
27         if err != nil {
28                 log.Fatal(err)
29         }
30         err = enc.SetComplexity(10)
31         if err != nil {
32                 log.Fatal(err)
33         }
34         err = enc.SetDTX(true)
35         if err != nil {
36                 log.Fatal(err)
37         }
38         err = enc.SetBitrate(vors.Bitrate)
39         if err != nil {
40                 log.Fatal(err)
41         }
42         return enc
43 }
44
45 func pcmConv(buf []byte, pcm []int16) {
46         for i := 0; i < len(pcm); i++ {
47                 buf[i*2+0] = byte((uint16(pcm[i]) & 0x00FF) >> 0)
48                 buf[i*2+1] = byte((uint16(pcm[i]) & 0xFF00) >> 8)
49         }
50 }