Пример #1
0
static void
test_multi_audio()
{
    int keep_going = 1;
    int i;

    if (devcount > 64) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
                devcount);
        devcount = 64;
    }

    spec.callback = play_through_once;

    SDL_memset(cbd, '\0', sizeof(cbd));
    SDL_Log("playing on all devices...\n");
    for (i = 0; i < devcount; i++) {
        const char *devname = SDL_GetAudioDeviceName(i, 0);
        spec.userdata = &cbd[i];
        cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
        if (cbd[i].dev == 0) {
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError());
        }
    }

    for (i = 0; i < devcount; i++) {
        if (cbd[i].dev) {
            SDL_PauseAudioDevice(cbd[i].dev, 0);
        }
    }

#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop(loop, 0, 1);
#else
    while (keep_going) {
        keep_going = 0;
        for (i = 0; i < devcount; i++) {
            if ((cbd[i].dev) && (!cbd[i].done)) {
                keep_going = 1;
            }
        }
        SDL_Delay(100);
    }
#endif
    for (i = 0; i < devcount; i++) {
        if (cbd[i].dev) {
            SDL_PauseAudioDevice(cbd[i].dev, 1);
            SDL_CloseAudioDevice(cbd[i].dev);
        }
    }

    SDL_Log("All done!\n");
}
Пример #2
0
bool GBASDLInitAudio(struct GBASDLAudio* context, struct GBAThread* threadContext) {
	if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
		GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system: %s", SDL_GetError());
		return false;
	}

	context->desiredSpec.freq = 44100;
	context->desiredSpec.format = AUDIO_S16SYS;
	context->desiredSpec.channels = 2;
	context->desiredSpec.samples = context->samples;
	context->desiredSpec.callback = _GBASDLAudioCallback;
	context->desiredSpec.userdata = context;
#if RESAMPLE_LIBRARY == RESAMPLE_NN
	context->drift = 0.f;
#endif

#if SDL_VERSION_ATLEAST(2, 0, 0)
	context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
	if (context->deviceId == 0) {
#else
	if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
#endif
		GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system");
		return false;
	}
	context->thread = threadContext;
	context->samples = context->obtainedSpec.samples;
	float ratio = GBAAudioCalculateRatio(0x8000, threadContext->fpsTarget, 44100);
	threadContext->audioBuffers = context->samples / ratio;
	if (context->samples > threadContext->audioBuffers) {
		threadContext->audioBuffers = context->samples * 2;
	}

#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_PauseAudioDevice(context->deviceId, 0);
#else
	SDL_PauseAudio(0);
#endif
	return true;
}

void GBASDLDeinitAudio(struct GBASDLAudio* context) {
	UNUSED(context);
#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_PauseAudioDevice(context->deviceId, 1);
	SDL_CloseAudioDevice(context->deviceId);
#else
	SDL_PauseAudio(1);
	SDL_CloseAudio();
#endif
	SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
Пример #3
0
/*
===========
S_Activate

Called when the main window gains or loses focus.
The window have been destroyed and recreated
between a deactivate and an activate.
===========
*/
void S_Activate (qboolean active)
{
	if (snd_init)
	{
		if ( active )
		{
			SDL_PauseAudioDevice(audio_device, 0);
		}
		else
		{
			SDL_PauseAudioDevice(audio_device, 1);
		}
	}
}
Пример #4
0
bool mSDLInitAudio(struct mSDLAudio* context, struct mCoreThread* threadContext) {
	if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
		mLOG(SDL_AUDIO, ERROR, "Could not initialize SDL sound system: %s", SDL_GetError());
		return false;
	}

	context->desiredSpec.freq = context->sampleRate;
	context->desiredSpec.format = AUDIO_S16SYS;
	context->desiredSpec.channels = 2;
	context->desiredSpec.samples = context->samples;
	context->desiredSpec.callback = _mSDLAudioCallback;
	context->desiredSpec.userdata = context;

#if SDL_VERSION_ATLEAST(2, 0, 0)
	context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
	if (context->deviceId == 0) {
#else
	if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) {
#endif
		mLOG(SDL_AUDIO, ERROR, "Could not open SDL sound system");
		return false;
	}
	context->core = 0;

	if (threadContext) {
		context->core = threadContext->core;
		context->sync = &threadContext->impl->sync;

#if SDL_VERSION_ATLEAST(2, 0, 0)
		SDL_PauseAudioDevice(context->deviceId, 0);
#else
		SDL_PauseAudio(0);
#endif
	}

	return true;
}

void mSDLDeinitAudio(struct mSDLAudio* context) {
	UNUSED(context);
#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_PauseAudioDevice(context->deviceId, 1);
	SDL_CloseAudioDevice(context->deviceId);
#else
	SDL_PauseAudio(1);
	SDL_CloseAudio();
#endif
	SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
Пример #5
0
static bool initAudio() {
	if (audioDev) {
		SDL_CloseAudioDevice(audioDev);
		audioDev = 0;
	}

	if (audioEnabled) {
		SDL_AudioSpec desired = {}, actual = {};
		// We want 48 KHz stereo f32 to match opusfile output
		desired.freq = 48000;
		desired.channels = 2;
		desired.format = AUDIO_F32;
		desired.samples = 4096; // TODO: user-configurable buffer size?
		desired.callback = audioCallback;

		audioDev = SDL_OpenAudioDevice(NULL, 0, &desired, &actual, 0);
		if (!audioDev) {
			narf::console->println("Could not open default audio device: " + std::string(SDL_GetError()));
			audioEnabled = false;
			return false;
		}

		SDL_PauseAudioDevice(audioDev, 0);

		narf::console->println("Audio initialized: " + std::to_string(actual.freq) + " Hz, " + std::to_string(actual.channels) + " channels");
	}

	return true;
}
Пример #6
0
void audio_pause() {
	// Pause the SDL audio device
	if (conf.audio_api == 0) { // SDL
		SDL_PauseAudioDevice(dev, 1);
	}
	paused = true;
}
Пример #7
0
static void SDLAudioCallback(void * /*userdata*/, Uint8 *stream, int len)
{
    if (playing_sample)
    {
        if (cursnd.sfxr)
        {
            uint l = len/2;
            float *fbuf = new float[l];
            memset(fbuf, 0, sizeof(float)*l);
            SynthSample(l, fbuf, nullptr);
            while (l--)
            {
                float f = fbuf[l];
                if (f < -1.0) f = -1.0;
                if (f > 1.0) f = 1.0;
                ((Sint16*)stream)[l] = (Sint16)(f * 32767);
            }
            delete[] fbuf;
        }
        else
        {
            size_t amount = min((size_t)len, cursnd.len - (cursndpos - cursnd.buf));
            memcpy(stream, cursndpos, amount);
            memset(stream + amount, 0, len - amount);
            cursndpos += amount;
            if (cursndpos == cursnd.buf + cursnd.len)
                playing_sample = false;
        }
    }
    else
    {
        memset(stream, 0, len);
        SDL_PauseAudioDevice(audioid, 1);
    }
}
Пример #8
0
void SoundSDL::resume()
{
	if (!_initialized)
		return;

	SDL_PauseAudioDevice(_dev, 0);
}
Пример #9
0
void SoundSDL::pause()
{
	if (!_initialized)
		return;

	SDL_PauseAudioDevice(_dev, 1);
}
Пример #10
0
void audio_unpause() {
	// Unpause the SDL audio device
	if (conf.audio_api == 0) { // SDL
		SDL_PauseAudioDevice(dev, 0);
	}
	paused = false;
}
Пример #11
0
void SdlAudioSink::Stop()
{
	if (this->state == Audio::State::STOPPED) return;

	SDL_PauseAudioDevice(this->device, 1);
	this->state = Audio::State::STOPPED;
}
Пример #12
0
void Mixer::Init(const char* device)
{
	Close();
	SDL_AudioSpec want, have;
	SDL_zero(want);
	want.freq = 44100;
	want.format = AUDIO_S16SYS;
	want.channels = 2;
	want.samples = 1024;
	want.callback = Callback;
	want.userdata = this;
	deviceid = SDL_OpenAudioDevice(device, 0, &want, &have, 0);
	format.format = have.format;
	format.channels = have.channels;
	format.freq = have.freq;
	const char* filename = get_file_path(PATH_ID_CSS1);
	for (size_t i = 0; i < Util::CountOf(css1sources); i++) {
		Source_Sample* source_sample = new Source_Sample;
		if (source_sample->LoadCSS1(filename, i)) {
			source_sample->Convert(format); // convert to audio output format, saves some cpu usage but requires a bit more memory, optional
			css1sources[i] = source_sample;
		} else {
			css1sources[i] = &source_null;
			delete source_sample;
		}
	}
	effectbuffer = new uint8[(have.samples * format.BytesPerSample() * format.channels)];
	SDL_PauseAudioDevice(deviceid, 0);
}
Пример #13
0
static void
iteration()
{
    SDL_Event e;
    SDL_AudioDeviceID dev;
    while (SDL_PollEvent(&e)) {
        if (e.type == SDL_QUIT) {
            done = 1;
        } else if (e.type == SDL_AUDIODEVICEADDED) {
            const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
            SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
            if (!e.adevice.iscapture) {
                positions[posindex] = 0;
                spec.userdata = &positions[posindex++];
                spec.callback = fillerup;
                dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
                if (!dev) {
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
                } else {
                    SDL_Log("Opened '%s' as %u\n", name, (unsigned int) dev);
                    SDL_PauseAudioDevice(dev, 0);
                }
            }
        } else if (e.type == SDL_AUDIODEVICEREMOVED) {
            dev = (SDL_AudioDeviceID) e.adevice.which;
            SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
            SDL_CloseAudioDevice(dev);
        }
    }
}
Пример #14
0
void SdlAudioSink::Start()
{
	if (this->state != Audio::State::STOPPED) return;

	SDL_PauseAudioDevice(this->device, 0);
	this->state = Audio::State::PLAYING;
}
Пример #15
0
void Audio::pause_audio()
{
    if (sound_enabled)
    {
        SDL_PauseAudioDevice(dev,1);
    }
}
Пример #16
0
bool ZWVideoThread::stop(bool isWait)
{

    if (mPlayerState == Stop)
    {
        return false;
    }

    mVideoState.quit = 1;

    mPlayerState = Stop;
    emit sig_StateChanged(Stop);

    if (isWait)
    {
        while(!mVideoState.readThreadFinished || !mVideoState.videoThreadFinished)
        {
            SDL_Delay(10);
        }
    }

    ///关闭SDL音频播放设备
    if (mVideoState.audioID != 0)
    {
        SDL_LockAudio();
        SDL_PauseAudioDevice(mVideoState.audioID,1);
        SDL_UnlockAudio();

        mVideoState.audioID = 0;
    }


    return true;
}
Пример #17
0
SDL2Sink::SDL2Sink() : impl(std::make_unique<Impl>()) {
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
        LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed");
        impl->audio_device_id = 0;
        return;
    }

    SDL_AudioSpec desired_audiospec;
    SDL_zero(desired_audiospec);
    desired_audiospec.format = AUDIO_S16;
    desired_audiospec.channels = 2;
    desired_audiospec.freq = native_sample_rate;
    desired_audiospec.samples = 1024;
    desired_audiospec.userdata = impl.get();
    desired_audiospec.callback = &Impl::Callback;

    SDL_AudioSpec obtained_audiospec;
    SDL_zero(obtained_audiospec);

    impl->audio_device_id = SDL_OpenAudioDevice(nullptr, false, &desired_audiospec, &obtained_audiospec, 0);
    if (impl->audio_device_id <= 0) {
        LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed");
        return;
    }

    impl->sample_rate = obtained_audiospec.freq;

    // SDL2 audio devices start out paused, unpause it:
    SDL_PauseAudioDevice(impl->audio_device_id, 0);
}
Пример #18
0
SDL2SoundCore::~SDL2SoundCore() {
  // Pause and close the audio device (allowing
  // loaded sounds to be freed)
  if (device != 0) {
    SDL_PauseAudioDevice(device, 1);
    SDL_CloseAudioDevice(device);
  }
}
Пример #19
0
void mSDLResumeAudio(struct mSDLAudio* context) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_PauseAudioDevice(context->deviceId, 0);
#else
	UNUSED(context);
	SDL_PauseAudio(0);
#endif
}
Пример #20
0
SdlAudioSink::~SdlAudioSink()
{
	if (this->device == 0) return;

	// Silence any currently playing audio.
	SDL_PauseAudioDevice(this->device, SDL_TRUE);
	SDL_CloseAudioDevice(this->device);
}
Пример #21
0
void Audio::resume_audio()
{
    if (sound_enabled)
    {
        clear_buffers();
        SDL_PauseAudioDevice(dev,0);
    }
}
Пример #22
0
static int S_CaptureDriverInit (int sampleRate)
{
	SDL_AudioDeviceID inputdevid = 0;
	SDL_AudioSpec desired, obtained;
	int ret = 0;
	const char *requested_device = NULL;

	if (SDL_WasInit (SDL_INIT_AUDIO) == 0)
		ret = SDL_InitSubSystem (SDL_INIT_AUDIO);

	if (ret == -1) {
		Con_Printf ("Couldn't initialize SDL audio: %s\n", SDL_GetError ());
		return false;
	}

	memset (&desired, 0, sizeof (desired));
	desired.freq = sampleRate;
	desired.samples = 64;
	desired.format = AUDIO_S16LSB;
	desired.channels = 1;

	/* Make audiodevice list start from index 1 so that 0 can be system default */
	if (s_inputdevice.integer > 0) {
		requested_device = SDL_GetAudioDeviceName (s_inputdevice.integer - 1, 0);
	}

	if ((inputdevid = SDL_OpenAudioDevice (requested_device, 1, &desired, &obtained, 0)) <= 0) {
		Com_Printf ("sound: couldn't open SDL audio: %s\n", SDL_GetError ());
		if (requested_device != NULL) {
			Com_Printf ("sound: retrying with default audio device\n");
			if ((inputdevid = SDL_OpenAudioDevice (NULL, 1, &desired, &obtained, 0)) <= 0) {
				Com_Printf ("sound: failure again, aborting...\n");
				return 0;
			}
			Cvar_LatchedSet (&s_inputdevice, "0");
		}
	}

	if (obtained.format != AUDIO_S16LSB) {
		Com_Printf ("SDL audio format %d unsupported.\n", obtained.format);
		SDL_CloseAudioDevice (inputdevid);
		inputdevid = 0;
		return 0;
	}

	if (obtained.channels != 1 && obtained.channels != 2) {
		Com_Printf ("SDL audio channels %d unsupported.\n", obtained.channels);
		SDL_CloseAudioDevice (inputdevid);
		inputdevid = 0;
		return 0;
	}

	Com_Printf ("Using SDL audio capture driver: %s @ %d Hz (samplerate %d)\n", SDL_GetCurrentAudioDriver (), obtained.freq, obtained.samples);
	SDL_PauseAudioDevice (inputdevid, 0);

	return inputdevid;
}
Пример #23
0
void SNDDMA_StopCapture(void)
{
#ifdef USE_SDL_AUDIO_CAPTURE
	if (sdlCaptureDevice)
	{
		SDL_PauseAudioDevice(sdlCaptureDevice, 1);
	}
#endif
}
Пример #24
0
void GBASDLPauseAudio(struct GBASDLAudio* context) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_PauseAudioDevice(context->deviceId, 1);
#else
	UNUSED(context);
	SDL_PauseAudio(1);
#endif

}
Пример #25
0
		void StringSynth::Stop() {
			SDL_PauseAudioDevice(device_ID, 1);
			SDL_CloseAudioDevice(device_ID);
			stopped = true;
			paused = false;
			time_elapsed = -1.0f;
			distance_struck = 0.0f;
			initial_offset = 0.0f;
		}
Пример #26
0
int main(int argc, char** argv) {
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    SDL_Window* win = SDL_CreateWindow("Bytebeat Visualizer", 0, 0, 800, 600, 0);
    SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

    SDL_AudioSpec want, have;
    SDL_AudioDeviceID dev;
    SDL_zero(want); SDL_zero(have);
    want.freq = 8000;
    want.format = AUDIO_U8;
    want.channels = 1;
    want.samples = 4096;
    want.callback = audio_callback;
    // save the current time
    want.userdata = malloc(sizeof(unsigned int));
    memset(want.userdata, 0, sizeof(unsigned int));
    // and save the pointer for further use
    unsigned int* t = want.userdata;
    dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);

    SDL_PauseAudioDevice(dev, 0);
    int running = 1;
    while (running) {
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            switch (e.type) {
                case SDL_QUIT:
                running = 0;
                break;
            }
        }

        SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
        SDL_RenderClear(ren);
        SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
        unsigned int i = (*t);
        i -= 8000;
        for (; i < *t; i++) {
            int x = 0;
            int sample_width = 1;
            SDL_RenderDrawLine(ren, x, 0, x + sample_width, 200);
            x++;
        }
        SDL_RenderPresent(ren);
    }

    SDL_CloseAudioDevice(dev);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}
Пример #27
0
void SNDDMA_StartCapture(void)
{
#ifdef USE_SDL_AUDIO_CAPTURE
	if (sdlCaptureDevice)
	{
		SDL_ClearQueuedAudio(sdlCaptureDevice);
		SDL_PauseAudioDevice(sdlCaptureDevice, 0);
	}
#endif
}
Пример #28
0
/* attenuation in dB */
void osd_set_mastervolume(int _attenuation)
{
	// clamp the attenuation to 0-32 range
	if (_attenuation > 0) {
		attenuation = 0;
	} else if (_attenuation < -32) {
		attenuation = -32;
	} else {
		attenuation = _attenuation;
	}

	if (Machine->sample_rate > 0) {
		if (attenuation == -32) {
			SDL_PauseAudioDevice(audio_device, 1);
			SDL_ClearQueuedAudio(audio_device);
		} else {
			SDL_PauseAudioDevice(audio_device, 0);
		}
	}
}
Пример #29
0
void Audio::stop_audio()
{
    if (sound_enabled)
    {
        sound_enabled = false;

        SDL_PauseAudioDevice(dev,1);
        SDL_CloseAudioDevice(dev);

        delete[] dsp_buffer;
        delete[] mix_buffer;
    }
}
Пример #30
0
BOOL ffplayer::Pause(DWORD nPause)
{
	if (m_bStepNext)
	{
		return false;
	}
	m_bPause = nPause;
	if (m_devID > 0)
	{
		SDL_PauseAudioDevice(m_devID, m_bPause);
	}
	return true;
}