예제 #1
0
func NewAudio() *Audio {
	as = sdl_audio.AudioSpec{
		Freq:        44100,
		Format:      sdl_audio.AUDIO_S16SYS,
		Channels:    1,
		Out_Silence: 0,
		Samples:     uint16(SampleSize),
		Out_Size:    0,
	}

	if sdl_audio.OpenAudio(&as, nil) < 0 {
		log.Fatal(sdl.GetError())
	}

	sdl_audio.PauseAudio(false)

	return &Audio{
		samples: make([]int16, SampleSize),
	}
}
예제 #2
0
// Opens SDL audio.
// If 'playbackFrequency' is 0, the frequency will be equivalent to PLAYBACK_FREQUENCY.
func NewSDLAudio(app *spectrum.Application, playbackFrequency uint, hqAudio bool) (*SDLAudio, error) {
	if playbackFrequency == 0 {
		playbackFrequency = PLAYBACK_FREQUENCY
	}

	if playbackFrequency < MIN_PLAYBACK_FREQUENCY {
		return nil, errors.New(fmt.Sprintf("playback frequency of %d Hz is too low", playbackFrequency))
	}

	// Open SDL audio
	var spec sdl_audio.AudioSpec
	{
		spec.Freq = int(playbackFrequency)
		spec.Format = sdl_audio.AUDIO_S16SYS
		spec.Channels = 1
		spec.Samples = uint16(2048 * float32(playbackFrequency) / PLAYBACK_FREQUENCY)
		if sdl_audio.OpenAudio(&spec, &spec) != 0 {
			return nil, errors.New(sdl.GetError())
		}
		if app.Verbose {
			app.PrintfMsg("%#v", spec)
		}
	}

	audio := &SDLAudio{
		data:                  make(chan *spectrum.AudioData),
		playback:              make(chan *spectrum.AudioData, 2*BUFSIZE_IDEAL), // Use a buffered Go channel
		playbackLoopFinished:  make(chan byte),
		forwarderLoopFinished: nil,
		sdlAudioUnpaused:      false,
		bufSize:               0,
		freq:                  uint(spec.Freq),
		virtualFreq:           uint(spec.Freq),
		hqAudio:               hqAudio,
	}

	go forwarderLoop(app.NewEventLoop(), audio)
	go playbackLoop(app, audio)

	return audio, nil
}
예제 #3
0
func (a *Audio) Run() {
	as := sdl_audio.AudioSpec{
		Freq:        44100,
		Format:      sdl_audio.AUDIO_S16SYS,
		Channels:    1,
		Out_Silence: 0,
		Samples:     uint16(SampleSize),
		Out_Size:    0,
	}

	if sdl_audio.OpenAudio(&as, nil) < 0 {
		log.Fatal(sdl.GetError())
	}

	sdl_audio.PauseAudio(false)

	for {
		select {
		case s := <-a.sample:
			a.AppendSample(s)
		}
	}
}
예제 #4
0
func NewAudio(frequency int, sampleSize int) (audio *SDLAudio, err error) {
	spec := sdl_audio.AudioSpec{
		Freq:        frequency,
		Format:      sdl_audio.AUDIO_S16SYS,
		Channels:    1,
		Out_Silence: 0,
		Samples:     uint16(sampleSize),
		Out_Size:    0,
	}

	if sdl_audio.OpenAudio(&spec, nil) < 0 {
		err = errors.New(sdl.GetError())
		return
	}

	sdl_audio.PauseAudio(false)

	audio = &SDLAudio{
		samples: make([]int16, sampleSize),
		input:   make(chan int16),
	}

	return
}