func (a *Audio) AppendSample(s int16) { a.samples[a.sampleIndex] = s a.sampleIndex++ if a.sampleIndex == SampleSize { sdl_audio.SendAudio_int16(a.samples) a.sampleIndex = 0 } }
func (audio *SDLAudio) Run() { i := 0 for { select { case s := <-audio.input: audio.samples[i] = s if i++; i == len(audio.samples) { sdl_audio.SendAudio_int16(audio.samples) i = 0 } } } }
func (audio *SDLAudio) render(audioData *spectrum.AudioData) { var events []spectrum.BeeperEvent if len(audioData.BeeperEvents) > 0 { var firstEvent *spectrum.BeeperEvent = &audioData.BeeperEvents[0] spectrum.Assert(firstEvent.TState == 0) var lastEvent *spectrum.BeeperEvent = &audioData.BeeperEvents[len(audioData.BeeperEvents)-1] spectrum.Assert(lastEvent.TState == spectrum.TStatesPerFrame) events = audioData.BeeperEvents } else { events = make([]spectrum.BeeperEvent, 2) events[0] = spectrum.BeeperEvent{TState: 0, Level: 0} events[1] = spectrum.BeeperEvent{TState: spectrum.TStatesPerFrame, Level: 0} } /* // A test signal const D = spectrum.TStatesPerFrame/128 events = make([]spectrum.BeeperEvent, spectrum.TStatesPerFrame/D+1) for i:=uint(0); i<128; i++ { events[i] = spectrum.BeeperEvent{TState: i*D, Level: uint8(i%2)} } events[len(events)-1] = spectrum.BeeperEvent{TState: spectrum.TStatesPerFrame, Level: 0} */ numEvents := len(events) spread := float64(audio.freq) / RESPONSE_FREQUENCY spread1 := 1 / spread var numSamples int var samples []float64 var samples_int16 []int16 var overflow []float64 { audio.mutex.Lock() numSamples_float := float32(audio.virtualFreq) / audioData.FPS numSamples = int(numSamples_float) len_overflow := int(math.Ceil(spread)) + 2 audio.numSamples_cummulativeFraction += numSamples_float - float32(numSamples) if audio.numSamples_cummulativeFraction >= 1.0 { numSamples += 1 audio.numSamples_cummulativeFraction -= 1.0 } if len(audio.samples) < numSamples+len_overflow { audio.samples = make([]float64, numSamples+len_overflow) } samples = audio.samples if len(audio.samples_int16) < numSamples { audio.samples_int16 = make([]int16, numSamples) } samples_int16 = audio.samples_int16 if len(audio.overflow) < len_overflow { new_overflow := make([]float64, len_overflow) copy(new_overflow, overflow) audio.overflow = new_overflow } overflow = audio.overflow audio.mutex.Unlock() } var k float64 = float64(numSamples) / spectrum.TStatesPerFrame { for i := 0; i < len(samples); i++ { samples[i] = 0 } copy(samples[:], overflow[:]) for i := 0; i < numEvents-1; i++ { start := events[i] end := events[i+1] level := float64(spectrum.Audio16_Table[start.Level]) var position0 float64 = float64(start.TState) * k var position1 float64 = float64(end.TState) * k if audio.hqAudio { add_hq(samples, position0+1, position1-position0, level, spread, spread1) } else { add_lq(samples, position0+1, position1-position0, level) } } copy(overflow[:], samples[numSamples:]) } for i := 0; i < numSamples; i++ { const VOLUME_ADJUSTMENT = 0.5 samples_int16[i] = int16(VOLUME_ADJUSTMENT * samples[i]) } audio.frame++ sdl_audio.SendAudio_int16(samples_int16[0:numSamples]) }