vorbisfile.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package ov implements the Go bindings of a subset of the functions of the Ogg Vorbis File C library.
  5. // The libvorbisfile C API reference is at: https://xiph.org/vorbis/doc/vorbisfile/reference.html
  6. package ov
  7. // #cgo darwin,amd64 CFLAGS: -DGO_DARWIN -I/usr/include/vorbis -I/usr/local/include/vorbis
  8. // #cgo darwin,arm64 CFLAGS: -DGO_DARWIN -I/opt/homebrew/include -I/opt/homebrew/include/vorbis
  9. // #cgo freebsd CFLAGS: -DGO_FREEBSD -I/usr/include/vorbis -I/usr/local/include/vorbis
  10. // #cgo linux CFLAGS: -DGO_LINUX -I/usr/include/vorbis
  11. // #cgo windows CFLAGS: -DGO_WINDOWS -I${SRCDIR}/../windows/libvorbis-1.3.5/include/vorbis -I${SRCDIR}/../windows/libogg-1.3.3/include
  12. // #cgo darwin,amd64 LDFLAGS: -L/usr/lib -L/usr/local/lib -lvorbisfile
  13. // #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/lib -lvorbisfile
  14. // #cgo freebsd LDFLAGS: -L/usr/lib -L/usr/local/lib -lvorbisfile
  15. // #cgo linux LDFLAGS: -lvorbisfile
  16. // #cgo windows LDFLAGS: -L${SRCDIR}/../windows/bin -llibvorbisfile
  17. // #include <stdlib.h>
  18. // #include "vorbisfile.h"
  19. import "C"
  20. import (
  21. "fmt"
  22. "unsafe"
  23. )
  24. // File type encapsulates a pointer to C allocated OggVorbis_File structure
  25. type File struct {
  26. vf *C.OggVorbis_File
  27. }
  28. type VorbisInfo struct {
  29. Version int
  30. Channels int
  31. Rate int
  32. BitrateUpper int
  33. BitrateNominal int
  34. BitrateLower int
  35. BitrateWindow int
  36. }
  37. const (
  38. Eread = C.OV_EREAD
  39. Efault = C.OV_EFAULT
  40. Eimpl = C.OV_EIMPL
  41. Einval = C.OV_EINVAL
  42. EnotVorbis = C.OV_ENOTVORBIS
  43. EbadHeader = C.OV_EBADHEADER
  44. Eversion = C.OV_EVERSION
  45. EnotAudio = C.OV_ENOTAUDIO
  46. EbadPacket = C.OV_EBADPACKET
  47. EbadLink = C.OV_EBADLINK
  48. EnoSeek = C.OV_ENOSEEK
  49. )
  50. // Maps ogg vorbis error codes to string
  51. var errCodes = map[C.int]string{
  52. C.OV_EREAD: "Eread",
  53. C.OV_EFAULT: "Efault",
  54. C.OV_EIMPL: "Eimpl",
  55. C.OV_EINVAL: "Einval",
  56. C.OV_ENOTVORBIS: "EnotVorbis",
  57. C.OV_EVERSION: "Eversion",
  58. C.OV_ENOTAUDIO: "EnotAudio",
  59. C.OV_EBADPACKET: "EbadPacket",
  60. C.OV_EBADLINK: "EbadLink",
  61. C.OV_ENOSEEK: "EnoSeek",
  62. }
  63. // Fopen opens an ogg vorbis file for decoding
  64. // Returns an opaque pointer to the internal decode structure and an error
  65. func Fopen(path string) (*File, error) {
  66. // Allocates pointer to vorbisfile structure using C memory
  67. var f File
  68. f.vf = (*C.OggVorbis_File)(C.malloc(C.size_t(unsafe.Sizeof(C.OggVorbis_File{}))))
  69. cpath := C.CString(path)
  70. defer C.free(unsafe.Pointer(cpath))
  71. cerr := C.ov_fopen(cpath, f.vf)
  72. if cerr == 0 {
  73. return &f, nil
  74. }
  75. return nil, fmt.Errorf("Error:%s from Fopen", errCodes[cerr])
  76. }
  77. // Clear clears the decoded buffers and closes the file
  78. func Clear(f *File) error {
  79. cerr := C.ov_clear(f.vf)
  80. if cerr == 0 {
  81. C.free(unsafe.Pointer(f.vf))
  82. f.vf = nil
  83. return nil
  84. }
  85. return fmt.Errorf("Error:%s from Clear", errCodes[cerr])
  86. }
  87. // Read decodes next data from the file updating the specified buffer contents and
  88. // returns the number of bytes read, the number of current logical bitstream and an error
  89. func Read(f *File, buffer unsafe.Pointer, length int, bigendianp bool, word int, sgned bool) (int, int, error) {
  90. var cbigendianp C.int = 0
  91. var csgned C.int = 0
  92. var bitstream C.int
  93. if bigendianp {
  94. cbigendianp = 1
  95. }
  96. if sgned {
  97. csgned = 1
  98. }
  99. cres := C.ov_read(f.vf, (*C.char)(buffer), C.int(length), cbigendianp, C.int(word), csgned, &bitstream)
  100. if cres < 0 {
  101. return 0, 0, fmt.Errorf("Error:%s from Read()", errCodes[C.int(cres)])
  102. }
  103. return int(cres), int(bitstream), nil
  104. }
  105. // Info updates the specified VorbisInfo structure with contains basic
  106. // information about the audio in a vorbis stream
  107. func Info(f *File, link int, info *VorbisInfo) error {
  108. vi := C.ov_info(f.vf, C.int(link))
  109. if vi == nil {
  110. return fmt.Errorf("Error returned from 'ov_info'")
  111. }
  112. info.Version = int(vi.version)
  113. info.Channels = int(vi.channels)
  114. info.Rate = int(vi.rate)
  115. info.BitrateUpper = int(vi.bitrate_upper)
  116. info.BitrateNominal = int(vi.bitrate_nominal)
  117. info.BitrateLower = int(vi.bitrate_lower)
  118. info.BitrateWindow = int(vi.bitrate_window)
  119. return nil
  120. }
  121. // Seekable returns indication whether or not the bitstream is seekable
  122. func Seekable(f *File) bool {
  123. cres := C.ov_seekable(f.vf)
  124. return cres != 0
  125. }
  126. // Seek seeks to the offset specified (in number pcm samples) within the physical bitstream.
  127. // This function only works for seekable streams.
  128. // Updates everything needed within the decoder, so you can immediately call Read()
  129. // and get data from the newly seeked to position.
  130. func PcmSeek(f *File, pos int64) error {
  131. cres := C.ov_pcm_seek(f.vf, C.ogg_int64_t(pos))
  132. if cres == 0 {
  133. return nil
  134. }
  135. return fmt.Errorf("Error:%s from 'ov_pcm_seek()'", errCodes[C.int(cres)])
  136. }
  137. // PcmTotal returns the total number of pcm samples of the physical bitstream or a specified logical bit stream.
  138. // To retrieve the total pcm samples for the entire physical bitstream, the 'link' parameter should be set to -1
  139. func PcmTotal(f *File, i int) (int64, error) {
  140. cres := C.ov_pcm_total(f.vf, C.int(i))
  141. if cres < 0 {
  142. return 0, fmt.Errorf("Error:%s from 'ov_pcm_total()'", errCodes[C.int(cres)])
  143. }
  144. return int64(cres), nil
  145. }
  146. // TimeTotal returns the total time in seconds of the physical bitstream or a specified logical bitstream
  147. // To retrieve the time total for the entire physical bitstream, 'i' should be set to -1.
  148. func TimeTotal(f *File, i int) (float64, error) {
  149. cres := C.ov_time_total(f.vf, C.int(i))
  150. if cres < 0 {
  151. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  152. }
  153. return float64(cres), nil
  154. }
  155. // TimeTell returns the current decoding offset in seconds.
  156. func TimeTell(f *File) (float64, error) {
  157. cres := C.ov_time_tell(f.vf)
  158. if cres < 0 {
  159. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  160. }
  161. return float64(cres), nil
  162. }