Пример #1
0
// NewDenseIIR wrapps a sound in an IIR filter, as specified by the coefficients.
// TODO(padster): Also implement the filter design algorithms, e.g:
//   http://engineerjs.com/?sidebar=docs/iir.html
//   http://www.mikroe.com/chapters/view/73/chapter-3-iir-filters/
//   http://www-users.cs.york.ac.uk/~fisher/mkfilter/
//
// For example, to use a high-pass filter for 800hz+ with sample rate of 44.1k:
//  sound := s.NewDenseIIR(...some sound...,
//    []float64{0.8922, -2.677, 2.677, -0.8922},
//    []float64{2.772, -2.57, 0.7961},
//  )
func NewDenseIIR(wrapped Sound, inCoef []float64, outCoef []float64) Sound {
	// TODO(padster): Verify this is doing what it should...hard to tell just by listening.
	data := denseIIR{
		wrapped,
		inCoef,
		outCoef,
		types.NewBuffer(len(inCoef)),
		types.NewBuffer(len(outCoef)),
	}
	return NewBaseSound(&data, wrapped.Length())
}
Пример #2
0
// RenderLinesWithEvents renders multiple channels of samples to screen, and draws events.
func (s *Screen) RenderLinesWithEvents(lines []Line, events <-chan interface{}, sampleRate int) {
	s.lines = lines

	runtime.LockOSThread()

	// NOTE: It appears that glfw 3.1 uses its own internal error callback.
	// glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
	// log.Fatalf("%v: %s\n", err, desc)
	// })
	if err := glfw.Init(); err != nil {
		log.Fatalf("Can't init glfw: %v!", err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(s.width, s.height, "Muse", nil, nil)
	if err != nil {
		log.Fatalf("CreateWindow failed: %s", err)
	}
	if aw, ah := window.GetSize(); aw != s.width || ah != s.height {
		log.Fatalf("Window doesn't have the requested size: want %d,%d got %d,%d", s.width, s.height, aw, ah)
	}
	window.MakeContextCurrent()

	// Must gl.Init() *after* MakeContextCurrent
	if err := gl.Init(); err != nil {
		log.Fatalf("Can't init gl: %v!", err)
	}

	// Set window up to be [0, -1.0] -> [width, 1.0], black.
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translated(-1, 0, 0)
	gl.Scaled(2/float64(s.width), 1.0, 1.0)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)

	// Actually start writing data to the buffer
	for i, _ := range s.lines {
		s.lines[i].valueBuffer = types.NewBuffer(int(float64(s.width) / s.pixelsPerSample))
		s.lines[i].valueBuffer.GoPushChannel(s.lines[i].Values, sampleRate)
	}
	if events != nil {
		s.eventBuffer.GoPushChannel(events, sampleRate)
	}

	// Keep drawing while we still can (and should).
	for !window.ShouldClose() && !s.bufferFinished() {
		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			break
		}
		gl.Clear(gl.COLOR_BUFFER_BIT)
		s.drawSignal()
		window.SwapBuffers()
		glfw.PollEvents()
	}

	// Keep window around, only close on esc.
	for !window.ShouldClose() && window.GetKey(glfw.KeyEscape) != glfw.Press {
		glfw.PollEvents()
	}
}