Skip to content

Commit

Permalink
add build tag for libasound
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannGh committed Nov 18, 2022
1 parent 50edb56 commit a5c20d4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 46 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ build-ebpf:
build:
go build -o $(NAME) .

build-with-sound:
go build -tags libasound -o $(NAME) .

all: build-ebpf build

clean:
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

"github.com/DataDog/dev/konamicode-ebpf/pkg/sound"
manager "github.com/DataDog/ebpf-manager"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -72,18 +73,18 @@ func openURL(url string) {
}
}

func playSong(sp *SineWavePlayer) {
func playSong(sp *sound.SineWavePlayer) {
const (
freqC = 523
freqE = 659
freqG = 784
)
sp.QueueNote(Note{freqC, 500})
sp.QueueNote(Note{freqE, 500})
sp.QueueNote(Note{freqG, 500})
sp.QueueNote(sound.Note{Freq: freqC, Duration: 500})
sp.QueueNote(sound.Note{Freq: freqE, Duration: 500})
sp.QueueNote(sound.Note{Freq: freqG, Duration: 500})
}

func start_konamicode_watcher(sp *SineWavePlayer) {
func start_konamicode_watcher(sp *sound.SineWavePlayer) {
go func() {
konamicode_check := time.NewTicker(time.Second)

Expand All @@ -110,7 +111,7 @@ func main() {
panic(fmt.Errorf("failed to init manager: %w", err))
}

sp, ready, err := NewSineWavePlayer(48000, 2, FormatSignedInt16LE)
sp, ready, err := sound.NewSineWavePlayer(48000, 2, sound.FormatSignedInt16LE)
if err != nil {
panic(fmt.Errorf(err.Error()))
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/sound/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sound

const (
// FormatFloat32LE is the format of 32 bits floats little endian.
FormatFloat32LE = 0

// FormatUnsignedInt8 is the format of 8 bits integers.
FormatUnsignedInt8 = 1

//FormatSignedInt16LE is the format of 16 bits integers little endian.
FormatSignedInt16LE = 2
)

type Note struct {
// Hz
Freq int64
// ms
Duration int64
}
39 changes: 39 additions & 0 deletions pkg/sound/sound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build !libasound
// +build !libasound

// Copyright 2019 The Oto Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sound

import (
"sync"
)

type SineWavePlayer struct{}

func NewSineWavePlayer(sampleRate int, channelCount int, format int) (*SineWavePlayer, chan struct{}, error) {
sp := &SineWavePlayer{}
ready := make(chan struct{}, 1)
ready <- struct{}{}
return sp, ready, nil
}

func (sp *SineWavePlayer) PlayLoop(wg *sync.WaitGroup) {
wg.Done()
}

func (sp *SineWavePlayer) QueueNote(n Note) {}

func (sp *SineWavePlayer) Close() {}
65 changes: 25 additions & 40 deletions sound.go → pkg/sound/sound_libasound.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build libasound
// +build libasound

// Copyright 2019 The Oto Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package sound

import (
"fmt"
Expand All @@ -24,17 +27,6 @@ import (
"github.com/hajimehoshi/oto/v2"
)

const (
// FormatFloat32LE is the format of 32 bits floats little endian.
FormatFloat32LE = 0

// FormatUnsignedInt8 is the format of 8 bits integers.
FormatUnsignedInt8 = 1

//FormatSignedInt16LE is the format of 16 bits integers little endian.
FormatSignedInt16LE = 2
)

func formatByteLength(format int) int {
switch format {
case FormatFloat32LE:
Expand All @@ -48,14 +40,7 @@ func formatByteLength(format int) int {
}
}

type Note struct {
// Hz
freq int64
// ms
duration int64
}

type SineWave struct {
type sineWave struct {
freq float64
length int64
pos int64
Expand All @@ -67,7 +52,7 @@ type SineWave struct {
remaining []byte
}

func (s *SineWave) Read(buf []byte) (int, error) {
func (s *sineWave) Read(buf []byte) (int, error) {
if len(s.remaining) > 0 {
n := copy(buf, s.remaining)
copy(s.remaining, s.remaining[n:])
Expand Down Expand Up @@ -150,25 +135,10 @@ type SineWavePlayer struct {
notesQueue chan Note
}

func NewSineWavePlayer(sampleRate int, channelCount int, format int) (*SineWavePlayer, chan struct{}, error) {
sp := &SineWavePlayer{
sampleRate: sampleRate,
channelCount: channelCount,
format: format,
notesQueue: make(chan Note),
}
ctx, ready, err := oto.NewContext(sp.sampleRate, sp.channelCount, sp.format)
if err != nil {
return nil, nil, err
}
sp.ctx = ctx
return sp, ready, nil
}

func (sp *SineWavePlayer) NewSineWave(freq float64, duration time.Duration) *SineWave {
func (sp *SineWavePlayer) newSineWave(freq float64, duration time.Duration) *sineWave {
l := int64(sp.channelCount) * int64(formatByteLength(sp.format)) * int64(sp.sampleRate) * int64(duration) / int64(time.Second)
l = l / 4 * 4
return &SineWave{
return &sineWave{
freq: freq,
length: l,
playerChannelCount: sp.channelCount,
Expand All @@ -178,13 +148,28 @@ func (sp *SineWavePlayer) NewSineWave(freq float64, duration time.Duration) *Sin
}

func (sp *SineWavePlayer) play(note Note) {
duration := time.Duration(note.duration) * time.Millisecond
p := sp.ctx.NewPlayer(sp.NewSineWave(float64(note.freq), duration))
duration := time.Duration(note.Duration) * time.Millisecond
p := sp.ctx.NewPlayer(sp.newSineWave(float64(note.Freq), duration))
p.Play()
time.Sleep(duration)
p.Close()
}

func NewSineWavePlayer(sampleRate int, channelCount int, format int) (*SineWavePlayer, chan struct{}, error) {
sp := &SineWavePlayer{
sampleRate: sampleRate,
channelCount: channelCount,
format: format,
notesQueue: make(chan Note),
}
ctx, ready, err := oto.NewContext(sp.sampleRate, sp.channelCount, sp.format)
if err != nil {
return nil, nil, err
}
sp.ctx = ctx
return sp, ready, nil
}

func (sp *SineWavePlayer) PlayLoop(wg *sync.WaitGroup) {
defer wg.Done()

Expand Down

0 comments on commit a5c20d4

Please sign in to comment.