LoginSignup
6
4

More than 3 years have passed since last update.

Goで音楽を奏でてみた

Posted at

Advent Calendar5日目に音声分析をGoでやろうとして挫折してしまいましたが、せめて音楽を再生できないか調べてみました。
https://qiita.com/usk81/items/8284ff74e79bdbd7dbae

pydub相当のパッケージは存在しませんが、音楽ファイルを再生できるぐらいのものはありました。

beep
https://github.com/faiface/beep

// package main plays two audio file
package main

import (
    "log"
    "os"
    "time"

    "github.com/faiface/beep"
    "github.com/faiface/beep/mp3"
    "github.com/faiface/beep/speaker"
)

func main() {
    f, err := os.Open("test.mp3")
    if err != nil {
        log.Fatal(err)
    }
    st, format, err := mp3.Decode(f)
    if err != nil {
        log.Fatal(err)
    }
    defer st.Close()

    speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

    done := make(chan bool)
    speaker.Play(beep.Seq(st, beep.Callback(func() {
        done <- true
    })))
    <-done
}

ちなみに音楽再生は oto というライブラリに依存しているものがほとんどでした。

oto
https://github.com/hajimehoshi/oto

hajimehoshi?
なんか聞いたことがある... (´ε`;)

golang.tokyoで何度か登壇されてたebiten作った人でした
https://github.com/hajimehoshi/ebiten

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4