File: g711enc.go

package info (click to toggle)
golang-github-zaf-g711 1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: makefile: 4
file content (81 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
	Copyright (C) 2016 - 2017, Lefteris Zafiris <zaf@fastmail.com>

	This program is free software, distributed under the terms of
	the BSD 3-Clause License. See the LICENSE file
	at the top of the source tree.

	g711enc encodes 16bit 8kHz LPCM data to 8bit G711 PCM.
	It works with wav or raw files as input.

*/

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/zaf/g711"
)

func main() {
	if len(os.Args) < 3 || os.Args[1] == "help" || os.Args[1] == "--help" || (os.Args[1] != "ulaw" && os.Args[1] != "alaw") {
		fmt.Printf("%s Encodes 16bit 8kHz LPCM data to 8bit G711 PCM\n", os.Args[0])
		fmt.Println("The program takes as input a list or wav or raw files, encodes them")
		fmt.Println("to G711 PCM and saves them with the proper extension.")
		fmt.Printf("\nUsage: %s [encoding format] [files]\n", os.Args[0])
		fmt.Println("encoding format can be either alaw or ulaw")
		os.Exit(1)
	}
	var exitCode int
	format := os.Args[1]
	for _, file := range os.Args[2:] {
		err := encodeG711(file, format)
		if err != nil {
			fmt.Println(err)
			exitCode = 1
		}
	}
	os.Exit(exitCode)
}

func encodeG711(file, format string) error {
	input, err := ioutil.ReadFile(file)
	if err != nil {
		return err
	}

	extension := strings.ToLower(filepath.Ext(file))
	if extension != ".wav" && extension != ".raw" && extension != ".sln" {
		err = fmt.Errorf("Unrecognised format for input file: %s", file)
		return err
	}
	outName := strings.TrimSuffix(file, filepath.Ext(file)) + "." + format
	outFile, err := os.Create(outName)
	if err != nil {
		return err
	}
	defer outFile.Close()
	encoder := new(g711.Encoder)
	if format == "alaw" {
		encoder, err = g711.NewAlawEncoder(outFile, g711.Lpcm)
		if err != nil {
			return err
		}
	} else {
		encoder, err = g711.NewUlawEncoder(outFile, g711.Lpcm)
		if err != nil {
			return err
		}
	}
	if extension == ".wav" {
		_, err = encoder.Write(input[44:]) // Skip WAV header
		return err
	}
	_, err = encoder.Write(input)
	return err
}