File: g711dec.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 (74 lines) | stat: -rw-r--r-- 1,710 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
/*
	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.

	g711dec decodes 8bit G711 PCM data to 16 Bit signed LPCM raw data

*/

package main

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

	"github.com/zaf/g711"
)

func main() {
	if len(os.Args) == 1 || os.Args[1] == "help" || os.Args[1] == "--help" {
		fmt.Printf("%s Decodes 8bit G711 PCM data to raw 16 Bit signed LPCM\n", os.Args[0])
		fmt.Println("The program takes as input a list A-law or u-law encoded files")
		fmt.Println("decodes them to LPCM and saves the files with a \".raw\" extension.")
		fmt.Printf("\nUsage: %s [files]\n", os.Args[0])
		os.Exit(1)
	}
	var exitCode int
	for _, file := range os.Args[1:] {
		err := decodeG711(file)
		if err != nil {
			fmt.Println(err)
			exitCode = 1
		}
	}
	os.Exit(exitCode)
}

func decodeG711(file string) error {
	input, err := os.Open(file)
	if err != nil {
		return err
	}
	defer input.Close()

	extension := strings.ToLower(filepath.Ext(file))
	decoder := new(g711.Decoder)
	if extension == ".alaw" || extension == ".al" {
		decoder, err = g711.NewAlawDecoder(input)
		if err != nil {
			return err
		}
	} else if extension == ".ulaw" || extension == ".ul" {
		decoder, err = g711.NewUlawDecoder(input)
		if err != nil {
			return err
		}
	} else {
		err = fmt.Errorf("Unrecognised format for file: %s", file)
		return err
	}
	outName := strings.TrimSuffix(file, filepath.Ext(file)) + ".raw"
	outFile, err := os.Create(outName)
	if err != nil {
		return err
	}
	defer outFile.Close()
	_, err = io.Copy(outFile, decoder)
	return err
}