Skip to content

Instantly share code, notes, and snippets.

@motoki317
Last active September 19, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motoki317/6a88b803f86a31480cf68a71bbf948f8 to your computer and use it in GitHub Desktop.
Save motoki317/6a88b803f86a31480cf68a71bbf948f8 to your computer and use it in GitHub Desktop.
水素の音
package main
import (
"fmt"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
"math"
"os"
)
func handle(err error) {
if err != nil {
panic(err)
}
}
const (
sampleRate = 48000
bitDepth = 16
length = 10
)
var (
format = audio.FormatMono48000
)
func main() {
f, err := os.Create("out.wav")
handle(err)
e := wav.NewEncoder(f, sampleRate, bitDepth, 1, 1)
data := make([]int, sampleRate * length)
for i := 0; i < sampleRate * length; i++ {
data[i] = int(multipleRydbergSeries(math.Pow(2, 15)-1, 880, float64(i) / sampleRate))
}
fmt.Println(data[:1000])
buf := &audio.IntBuffer{
Format: format,
Data: data,
SourceBitDepth: bitDepth,
}
handle(e.Write(buf))
handle(e.Close())
}
func multipleRydbergSeries(amplitude, baseFreq, t float64) float64 {
const count = 10
var f float64
for i := 1; i <= count; i++ {
f += rydbergSeries(amplitude, baseFreq, float64(i), t)
}
return f / count
}
func rydbergSeries(amplitude, baseFreq, n, t float64) float64 {
const count = 10
var f float64
for i := 1; i <= count; i++ {
f += rydberg(amplitude, baseFreq, n, n+float64(i), t)
}
return f / count
}
func rydberg(amplitude, baseFreq, n1, n2, t float64) float64 {
return sinWave(amplitude, baseFreq * (1 / n1 / n1 - 1 / n2 / n2), t)
}
func sinWave(amplitude, frequency, t float64) float64 {
return amplitude * math.Sin(frequency * 2 * math.Pi * t)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment