Пример #1
0
func lolwut() {
	runtime.LockOSThread()
	var stream unsafe.Pointer
	err := C.Pa_OpenDefaultStream(&stream,
		0,           // no input
		1,           // mono
		C.paFloat32, // 32-bit floating point
		48000,
		C.paFramesPerBufferUnspecified,
		nil,
		nil)
	if err != C.paNoError {
		panic(C.GoString(C.Pa_GetErrorText(err)))
	}
	err = C.Pa_StartStream(stream)
	if err != C.paNoError {
		panic(C.GoString(C.Pa_GetErrorText(err)))
	}
	for {
		err := C.Pa_WriteStream(stream, unsafe.Pointer(&genSine(440)[0]), 48000)
		if err != C.paNoError {
			panic(C.GoString(C.Pa_GetErrorText(err)))
		}
	}
}
Пример #2
0
func (s *Stream) Start() error {
	err := C.Pa_StartStream(s.paStream)
	if err != C.paNoError {
		return newError(err)
	}
	return nil
}
Пример #3
0
func OpenStream(inOpts *InputOptions,
	outOpts *OutputOptions,
	sampleRate float64) (strm *Stream, err error) {
	// initialize return value first
	strm = new(Stream)
	strm.inopts = inOpts
	strm.outopts = outOpts
	// initialize in and out
	var _inopts *C.PaStreamParameters
	var _outopts *C.PaStreamParameters
	if inOpts != nil {
		_inopts = new(C.PaStreamParameters)
		_inopts.device = C.Pa_GetDefaultInputDevice()
		if inOpts.DeviceID != DefaultDevice {
			_inopts.device = C.PaDeviceIndex(inOpts.DeviceID)
		}
		_inopts.channelCount = C.int(inOpts.ChannelCnt)
		_inopts.sampleFormat = C.paFloat32
		_inopts.suggestedLatency = C.Pa_GetDeviceInfo(_inopts.device).defaultHighInputLatency
		_inopts.hostApiSpecificStreamInfo = nil
	}
	if outOpts != nil {
		_outopts = new(C.PaStreamParameters)
		_outopts.device = C.Pa_GetDefaultOutputDevice()
		if outOpts.DeviceID != DefaultDevice {
			_outopts.device = C.PaDeviceIndex(outOpts.DeviceID)
		}
		_outopts.channelCount = C.int(outOpts.ChannelCnt)
		_outopts.sampleFormat = C.paFloat32
		_outopts.suggestedLatency = C.Pa_GetDeviceInfo(_outopts.device).defaultHighOutputLatency
		_outopts.hostApiSpecificStreamInfo = nil
	}
	cerr := C.Pa_OpenStream(
		&strm.cee,
		_inopts,
		_outopts,
		C.double(sampleRate),
		C.paClipOff,
		0,
		nil,
		nil,
	)
	if cerr != C.paNoError {
		err = errors.New(C.GoString(C.Pa_GetErrorText(cerr)))
		return
	}
	cerr = C.Pa_StartStream(strm.cee)
	if cerr != C.paNoError {
		err = errors.New(C.GoString(C.Pa_GetErrorText(cerr)))
		return
	}
	return
}
Пример #4
0
// Start commences audio processing.
func (s *Stream) Start() error {
	return newError(C.Pa_StartStream(s.paStream))
}
Пример #5
0
func main() {
	runtime.GOMAXPROCS(32)

	// sdl
	C.SDL_Init(C.SDL_INIT_AUDIO | C.SDL_INIT_VIDEO | C.SDL_INIT_TIMER)
	defer C.SDL_Quit()
	runtime.LockOSThread()
	window := C.SDL_CreateWindow(C.CString("play"), 0, 0, 1680, 1050,
		C.SDL_WINDOW_BORDERLESS|C.SDL_WINDOW_RESIZABLE|C.SDL_WINDOW_MAXIMIZED|C.SDL_WINDOW_OPENGL)
	if window == nil {
		fatalSDLError()
	}
	defer C.SDL_DestroyWindow(window)
	C.SDL_DisableScreenSaver()
	renderer := C.SDL_CreateRenderer(window, -1, C.SDL_RENDERER_ACCELERATED)
	if renderer == nil {
		fatalSDLError()
	}
	defer C.SDL_DestroyRenderer(renderer)
	var width, height C.int
	C.SDL_GetWindowSize(window, &width, &height)
	texture := C.SDL_CreateTexture(renderer,
		C.SDL_PIXELFORMAT_YV12,
		C.SDL_TEXTUREACCESS_STREAMING,
		width, height)
	if texture == nil {
		fatalSDLError()
	}
	defer C.SDL_DestroyTexture(texture)

	// sdl ttf
	if C.TTF_Init() == C.int(-1) {
		log.Fatal("sdl ttf init failed")
	}
	defer C.TTF_Quit()
	font := C.TTF_OpenFont(C.CString("/home/reus/font.ttf"), 32)
	if font == nil {
		fatalTTFError()
	}
	defer C.TTF_CloseFont(font)

	// open decoder
	decoder, err := NewDecoder(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	if len(decoder.AudioStreams) == 0 || len(decoder.VideoStreams) == 0 {
		log.Fatal("no video or audio")
	}
	defer decoder.Close()

	// audio
	aCodecCtx := decoder.AudioStreams[0].codec
	audioStream := setupAudioOutput(int(aCodecCtx.sample_rate), int(aCodecCtx.channels), decoder)

	// call closure in sdl thread
	callEventCode := C.SDL_RegisterEvents(1)
	callbacks := make(chan func(Env), 1024)
	call := func(f func(env Env)) {
		var event C.SDL_Event
		var userevent C.SDL_UserEvent
		userevent._type = C.SDL_USEREVENT
		userevent.code = C.Sint32(callEventCode)
		C.set_userevent(&event, userevent)
		callbacks <- f
		C.SDL_PushEvent(&event)
	}

	// show fps
	nFrames := 0
	var fpsTexture *C.SDL_Texture
	var fpsColor C.SDL_Color
	var fpsSrc, fpsDst C.SDL_Rect
	fpsColor.r = 255
	fpsColor.g = 255
	fpsColor.b = 255
	fpsColor.a = 0
	go func() {
		for _ = range time.NewTicker(time.Second * 1).C {
			call(func(env Env) {
				cText := C.CString(fmt.Sprintf("%d", nFrames))
				sur := C.TTF_RenderUTF8_Blended(font, cText, fpsColor)
				fpsSrc.w = sur.w
				fpsSrc.h = sur.h
				fpsDst.w = sur.w
				fpsDst.h = sur.h
				C.SDL_DestroyTexture(fpsTexture)
				fpsTexture = C.SDL_CreateTextureFromSurface(env.renderer, sur)
				C.SDL_FreeSurface(sur)
				C.free(unsafe.Pointer(cText))
				nFrames = 0
			})
		}
	}()

	// render
	go func() {
		for {
			frame := <-decoder.timedFrames
			nFrames++
			call(func(env Env) {
				C.SDL_UpdateYUVTexture(env.texture, nil,
					(*C.Uint8)(unsafe.Pointer(frame.data[0])), frame.linesize[0],
					(*C.Uint8)(unsafe.Pointer(frame.data[1])), frame.linesize[1],
					(*C.Uint8)(unsafe.Pointer(frame.data[2])), frame.linesize[2])
				C.SDL_RenderCopy(env.renderer, env.texture, nil, nil)
				C.SDL_RenderCopy(env.renderer, fpsTexture, &fpsSrc, &fpsDst)
				C.SDL_RenderPresent(env.renderer)
				decoder.RecycleFrame(frame)
			})
		}
	}()

	// start decode
	decoder.Start(decoder.VideoStreams[0], decoder.AudioStreams[0], width, height)
	ret := C.Pa_StartStream(audioStream)
	if ret != C.paNoError {
		fatalPAError(ret)
	}

	// main loop
	var ev C.SDL_Event
	env := Env{
		window:   window,
		renderer: renderer,
		texture:  texture,
	}
	for {
		if C.SDL_WaitEvent(&ev) == C.int(0) {
			fatalSDLError()
		}
		switch C.get_event_type(&ev) {
		case C.SDL_QUIT:
			os.Exit(0)
		case C.SDL_KEYDOWN:
			key := C.get_event_key(&ev)
			switch key.keysym.sym {
			case C.SDLK_q: // quit
				os.Exit(0)
			case C.SDLK_f: // forward
				decoder.Seek(time.Second * 60)
			}
		case C.SDL_USEREVENT:
			if C.get_userevent_code(&ev) == callEventCode {
				(<-callbacks)(env)
			}
		}
	}
}
Пример #6
0
// StartStream function as declared in portaudio/portaudio.h:937
func StartStream(stream *Stream) Error {
	cstream, _ := (unsafe.Pointer)(unsafe.Pointer(stream)), cgoAllocsUnknown
	__ret := C.Pa_StartStream(cstream)
	__v := (Error)(__ret)
	return __v
}