예제 #1
0
파일: audio.go 프로젝트: willemvds/sdl
func OpenAudio(des *AudioSpec) (*AudioSpec, error) {
	var o *AudioSpec
	if C.SDL_OpenAudio(des.c(), o.c()) != 0 {
		return nil, getError()
	}

	return o, nil
}
예제 #2
0
파일: audio.go 프로젝트: neagix/Go-SDL
func OpenAudio(desired, obtained_orNil *AudioSpec) int {
	if alreadyOpened {
		panic("more than 1 audio stream currently not supported")
	}

	// copy handle to user-defined callback function, if defined
	// it is perfectly supported to not specify the callback function
	// in that case you will use default SendAudio semantics
	// note that if you specify a callback and use SendAudio, a hangup will instead happen
	// when calling SendAudio

	if nil != desired.UserDefinedCallback {
		userDefinedCallback = desired.UserDefinedCallback
	} else {
		// default playback (16-bit signed)
		userDefinedCallback = DownstreamPlaybackS16
		PlayLoop = make(chan AudioEvent)
	}

	var C_desired, C_obtained *C.SDL_AudioSpec

	C_desired = new(C.SDL_AudioSpec)
	C_desired.freq = C.int(desired.Freq)
	C_desired.format = C.Uint16(desired.Format)
	C_desired.channels = C.Uint8(desired.Channels)
	C_desired.samples = C.Uint16(desired.Samples)
	// there is an unique C callback acting as proxy to the different Go callbacks
	// see streamContext()
	C_desired.callback = C.callback_getCallback()

	if obtained_orNil != nil {
		if desired != obtained_orNil {
			C_obtained = new(C.SDL_AudioSpec)
		} else {
			C_obtained = C_desired
		}
	}

	status := C.SDL_OpenAudio(C_desired, C_obtained)

	if status == 0 {
		alreadyOpened = true
	}

	if obtained_orNil != nil {
		obtained := obtained_orNil

		obtained.Freq = int(C_obtained.freq)
		obtained.Format = uint16(C_obtained.format)
		obtained.Channels = uint8(C_obtained.channels)
		obtained.Samples = uint16(C_obtained.samples)
		obtained.Out_Silence = uint8(C_obtained.silence)
		obtained.Out_Size = uint32(C_obtained.size)
	}

	return int(status)
}
예제 #3
0
파일: audio.go 프로젝트: velour/ui
func initAudio() {
	var want C.SDL_AudioSpec
	want.freq = 44100
	want.format = C.AUDIO_S16LSB
	want.samples = 8096
	C.setCallback(&want)
	C.SDL_OpenAudio(&want, &openedSpec)
	C.SDL_PauseAudio(0)
}
예제 #4
0
파일: audio.go 프로젝트: jbondeson/Go-SDL2
func OpenAudio(desired, obtained *AudioSpec, callback func(*byte, int)) bool {
	var cdesired C.SDL_AudioSpec
	var cobtained C.SDL_AudioSpec
	cdesired.freq = C.int(desired.Freq)
	cdesired.format = C.SDL_AudioFormat(int(desired.Format))
	cdesired.channels = C.Uint8(desired.Channels)
	cdesired.samples = C.Uint16(desired.Samples)
	cdesired.size = C.Uint32(0)
	cdesired.callback = C.go_sdl2_get_callback()
	cdesired.userdata = unsafe.Pointer(&callback)
	ret := C.SDL_OpenAudio(&cdesired, &cobtained)
	if obtained != nil {
		obtained.Freq = int32(cobtained.freq)
		obtained.Format = AudioFormat(int(cobtained.format))
		obtained.Channels = uint8(cobtained.channels)
		obtained.Silence = uint8(cobtained.silence)
		obtained.Samples = uint16(cobtained.samples)
		obtained.Size = uint32(cobtained.size)
	}
	return int(ret) == 0
}
예제 #5
0
파일: audio.go 프로젝트: scottferg/Go-SDL
func OpenAudio(desired, obtained_orNil *AudioSpec) int {
	var C_desired, C_obtained *C.SDL_AudioSpec

	C_desired = new(C.SDL_AudioSpec)
	C_desired.freq = C.int(desired.Freq)
	C_desired.format = C.Uint16(desired.Format)
	C_desired.channels = C.Uint8(desired.Channels)
	C_desired.samples = C.Uint16(desired.Samples)
	C_desired.callback = C.callback_getCallback()

	if obtained_orNil != nil {
		if desired != obtained_orNil {
			C_obtained = new(C.SDL_AudioSpec)
		} else {
			C_obtained = C_desired
		}
	}

	status := C.SDL_OpenAudio(C_desired, C_obtained)

	if status == 0 {
		mutex.Lock()
		opened++
		mutex.Unlock()
	}

	if obtained_orNil != nil {
		obtained := obtained_orNil

		obtained.Freq = int(C_obtained.freq)
		obtained.Format = uint16(C_obtained.format)
		obtained.Channels = uint8(C_obtained.channels)
		obtained.Samples = uint16(C_obtained.samples)
		obtained.Out_Silence = uint8(C_obtained.silence)
		obtained.Out_Size = uint32(C_obtained.size)
	}

	return int(status)
}
예제 #6
0
파일: audio.go 프로젝트: flazz/go-sdl2
// OpenAudio (https://wiki.libsdl.org/SDL_OpenAudio)
func OpenAudio(desired, obtained *AudioSpec) int {
	return int(C.SDL_OpenAudio(desired.cptr(), obtained.cptr()))
}
예제 #7
0
파일: audio.go 프로젝트: tanema/go-sdl2
// OpenAudio (https://wiki.libsdl.org/SDL_OpenAudio)
func OpenAudio(desired, obtained *AudioSpec) error {
	if C.SDL_OpenAudio(desired.cptr(), obtained.cptr()) != 0 {
		return GetError()
	}
	return nil
}
예제 #8
0
func OpenAudio(desired, obtained *AudioSpec) int {
	_desired := (*C.SDL_AudioSpec)(unsafe.Pointer(desired))
	_obtained := (*C.SDL_AudioSpec)(unsafe.Pointer(obtained))
	return (int)(C.SDL_OpenAudio(_desired, _obtained))
}
예제 #9
0
파일: audio.go 프로젝트: beoran/fungo
// This function opens the audio device with the desired parameters, and
// returns 0 if successful, placing the actual hardware parameters in the
// structure pointed to by 'obtained'.  If 'obtained' is NULL, the audio
// data passed to the callback function will be guaranteed to be in the
// requested format, and will be automatically converted to the hardware
// audio format if necessary.  This function returns -1 if it failed
// to open the audio device, or couldn't set up the audio thread.
//
// When filling in the desired audio spec structure,
//  'desired->freq' should be the desired audio frequency in samples-per-second.
//  'desired->format' should be the desired audio format.
//  'desired->samples' is the desired size of the audio buffer, in samples.
//     This number should be a power of two, and may be adjusted by the audio
//     driver to a value more suitable for the hardware.  Good values seem to
//     range between 512 and 8096 inclusive, depending on the application and
//     CPU speed.  Smaller values yield faster response time, but can lead
//     to underflow if the application is doing heavy processing and cannot
//     fill the audio buffer in time.  A stereo sample consists of both right
//     and left channels in LR ordering.
//     Note that the number of samples is directly related to time by the
//     following formula:  ms = (samples*1000)/freq
//  'desired->size' is the size in bytes of the audio buffer, and is
//     calculated by SDL_OpenAudio().
//  'desired->silence' is the value used to set the buffer to silence,
//     and is calculated by SDL_OpenAudio().
//  'desired->callback' should be set to a function that will be called
//     when the audio device is ready for more data.  It is passed a pointer
//     to the audio buffer, and the length in bytes of the audio buffer.
//     This function usually runs in a separate thread, and so you should
//     protect data structures that it accesses by calling SDL_LockAudio()
//     and SDL_UnlockAudio() in your code.
//  'desired->userdata' is passed as the first parameter to your callback
//     function.
//
// The audio device starts out playing silence when it's opened, and should
// be enabled for playing by calling SDL_PauseAudio(0) when you are ready
// for your audio callback function to be called.  Since the audio driver
// may modify the requested size of the audio buffer, you should allocate
// any local mixing buffers after you open the audio device.
///
func OpenAudio(desired, obtained *C.SDL_AudioSpec) int {
	res := C.SDL_OpenAudio(desired, obtained)
	return int(res)
}