Esempio n. 1
0
// NewScreen creates a new output screen of a given size and sample density.
func NewScreen(width int, height int, samplesPerPixel int) *Screen {
	s := Screen{
		width,
		height,
		1.0 / float64(samplesPerPixel),
		types.NewTypedBuffer(width * samplesPerPixel),
		nil, // lines
	}
	return &s
}
Esempio n. 2
0
// NewScreen creates a new output screen of a given size and sample density.
func NewSpectrogramScreen(width int, height int, bpo int) *SpectrogramScreen {
	samplesPerPixel := 1 // HACK - parameterize?
	s := SpectrogramScreen{
		width,
		height,
		bpo,
		1.0 / float64(samplesPerPixel),
		types.NewTypedBuffer(width * samplesPerPixel),
		nil, // lines
	}
	return &s
}
Esempio n. 3
0
// RenderLinesWithEvents renders multiple channels of samples to screen, and draws events.
func (s *SpectrogramScreen) RenderLineWithEvents(line *ComplexLine, events <-chan interface{}, sampleRate int) {
	s.line = line

	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, "Spectrogram", 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, 0] -> [width, height], black.
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translated(-1, -1, 0)
	gl.Scaled(2/float64(s.width), 2/float64(s.height), 1.0)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)

	gl.ShadeModel(gl.FLAT)

	// Actually start writing data to the buffer
	s.line.valueBuffer = types.NewTypedBuffer(int(float64(s.width) / s.pixelsPerSample))
	s.line.valueBuffer.GoPushChannel(hackWrapChannel(s.line.Values), sampleRate)
	if events != nil {
		s.eventBuffer.GoPushChannel(events, sampleRate)
	}

	gl.Hint(gl.POINT_SMOOTH_HINT, gl.FASTEST)

	// 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()
	}
}