Skip to content

skit-ai/go-dub

 
 

Repository files navigation

godub

godub lets you manipulate audio in an easy and elegant way. It's ported from the excellent project pydub.

Why

There are some audio packages in the Go world, but we believe that pydub provides a better way to do stuff to audios. Therefore, here we have godub! However, due to limited time and personal ability, not all the features of pydub are supported.

Features

  • Load audio files, supports mp3/m4a/wav...
  • Export/convert audio with custom configs.
  • Slice an audio.
  • Concatenate two or more audios.
  • Repeat an audio.
  • Overlay with other audios.
  • Reverse an audio.
  • ...

Quickstart

Load

func main() {
	filePath := path.Join(dataDirectory(), "code-geass.mp3")
	segment, _ := godub.NewLoader().Load(filePath)
	fmt.Println(segment)

	buf, _ := os.ReadAll()

	// Load from buffer, load also accepts `io.Reader` type.
	segment, _ = godub.NewLoader().Load(bytes.NewReader(buf))
	fmt.Println(segment)
}

Export

func main() {
	filePath := path.Join(dataDirectory(), "code-geass.mp3")
	segment, _ := godub.NewLoader().Load(filePath)

	// Export as a mp3 file.
	toFilePath := path.Join(dataDirectory(), "converted", "code-geass.mp3")
	err := godub.NewExporter(toFilePath).
		WithDstFormat("mp3").
		WithBitRate(128000).
		Export(segment)
	if err != nil {
		log.Fatal(err)
	}

	// Let's check it.
	segment, _ = godub.NewLoader().Load(toFilePath)
	fmt.Println(segment)
}

Convert

func main() {
	filePath := path.Join(dataDirectory(), "code-geass.mp3")
	toFilePath := path.Join(dataDirectory(), "converted", "code-geass.m4a")
	w, _ := os.Create(toFilePath)

	err := audio.NewConverter(w).
		WithBitRate(64000).
		WithDstFormat("m4a").
		Convert(filePath)
	if err != nil {
		log.Fatal(err)
	}

	segment, _ := godub.NewLoader().Load(toFilePath)
	fmt.Println(segment)
}

Slice

func slice() {
	filePath := path.Join(dataDirectory(), "code-geass.mp3")
	segment, _ := godub.NewLoader().Load(filePath)

	// Slice from 0 sec to 100 secs.
	slicedSegment, _ := segment.Slice(0, 100*time.Second)

	// Then export it as mp3 file.
	slicedPath := path.Join(dataDirectory(), "slice", "code-geass.mp3")
	godub.NewExporter(slicedPath).WithDstFormat("mp3").Export(slicedSegment)
}

Concatenate

func main() {
	filePath := path.Join(dataDirectory(), "code-geass.mp3")
	segment, _ := godub.NewLoader().Load(filePath)

	// Yep, you can append more than one segment.
	newSeg, err := segment.Append(segment, segment)
	if err != nil {
		log.Fatal(err)
	}

	// Save the newly created audio segment as mp3 file.
	newPth := path.Join(dataDirectory(), "append", "code-geass.mp3")
	godub.NewExporter(newPth).WithDstFormat("mp3").WithBitRate(audio.MP3BitRatePerfect).Export(newSeg)
}

Overlay

func main() {
	segment, _ := godub.NewLoader().Load(path.Join(dataDirectory(), "code-geass.mp3"))
	otherSegment, _ := godub.NewLoader().Load(path.Join(dataDirectory(), "ring.mp3"))

	overlaidSeg, err := segment.Overlay(otherSegment, &godub.OverlayConfig{LoopToEnd: true})
	if err != nil {
		log.Fatal(err)
	}

	godub.NewExporter(destPath).WithDstFormat("wav").Export(path.Join(tmpDataDirectory(), "overlay-ring.wav"))
}

Dependency

godub uses ffmpeg as its backend to support encoding, decoding and conversion.

References

  1. go-binary-pack
  2. Python struct
  3. Digital Audio - Creating a WAV (RIFF) file
  4. ffmpeg tutorial
  5. Python: manipulate raw audio data with audioop
  6. ffmpeg Documentation

Similar Projects

  1. cryptix/wav
  2. mdlayher/waveform
  3. go-audio

TODO

  • Audio effects.
  • As always, test test test!

License

godub is licensed under the MIT license. Please feel free and have fun.

About

Manipulate audio in a simple and elegant way.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%