vorbisfile.go 5.5 KB

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