//go:build !disable_format_aac && disable_codec_libfdk_aac && cgo package aac import ( "fmt" "git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/vector" "io" ) type Format struct { } func NewFormat() Format { return Format{} } func (f Format) Name() string { return "aac" } func (f Format) EncoderDescription() string { return "vo-aacenc (S.O.N.G/voaac-go)" } func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[string]interface{}) error { var bitrate = 128 if options != nil { var val interface{} var ok bool var intVal int var strVal string if val, ok = options["bitrate"]; ok { if strVal, ok = val.(string); ok { switch strVal { case "320k": bitrate = 320 case "256k": bitrate = 256 case "192k": bitrate = 192 case "128k": bitrate = 128 case "64k": bitrate = 64 default: return fmt.Errorf("unknown setting bitrate=%s", strVal) } } else if intVal, ok = val.(int); ok { bitrate = intVal } } if val, ok = options["format"]; ok { if strVal, ok = val.(string); ok { if strVal != "adts" { return fmt.Errorf("format %s not supported", strVal) } } } } encoder, err := aac.NewEncoder(writer, &aac.Options{ BitRate: bitrate * 1024, NumChannels: source.GetChannels(), SampleRate: source.GetSampleRate(), }) if err != nil { return err } defer encoder.Close() pReader, pWriter := io.Pipe() defer pWriter.Close() go func() { defer pWriter.Close() for block := range source.ToInt16().GetBlocks() { if _, decodeErr := pWriter.Write(vector.Int16ToBytes(block)); decodeErr != nil { return } } }() if err = encoder.Encode(pReader); err != nil { return err } return nil } func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool { return extension == "aac" || extension == "adts" }