Skip to content

Commit

Permalink
SDL: Port to SDL2.
Browse files Browse the repository at this point in the history
First stages of porting MilkyTracker to SDL2, plus several minor
enhancements along the way. Please see notes in each individual file.
Requires X-Y mousewheel support in core MilkyTracker files, see
following commit.
  • Loading branch information
dwhinham authored and Deltafire committed Jul 6, 2015
1 parent 78386ff commit 12cce1d
Show file tree
Hide file tree
Showing 10 changed files with 962 additions and 797 deletions.
172 changes: 86 additions & 86 deletions src/milkyplay/drivers/sdl/AudioDriver_SDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/*
*
* AudioDriver_SDL.cpp
* MilkyPlay
*
* Created by Peter Barth on 09.06.05.
*
* Christopher O'Neill 16/3/06:
* fill_audio() rewritten to solve issues on certain systems
* 12/5/14 - Dale Whinham
* - Port to SDL2
*
* 16/3/06 - Christopher O'Neill
* - fill_audio() rewritten to solve issues on certain systems
*
*/

#include "AudioDriver_SDL.h"

void SDLCALL AudioDriver_SDL::fill_audio(void *udata, Uint8 *stream, int length)
{
AudioDriver_SDL* audioDriver = (AudioDriver_SDL*)udata;

if(length>>2 != audioDriver->periodSize)
{
fprintf(stderr, "SDL: Invalid buffer size: %i (should be %i), skipping..\n", length >> 2, audioDriver->periodSize);
}
// See comment in AudioDriver_ALSA.cpp
else
{
audioDriver->fillAudioWithCompensation((char*)stream, length);
}
AudioDriver_SDL* audioDriver = (AudioDriver_SDL*)udata;

if(length>>2 != audioDriver->periodSize)
{
fprintf(stderr, "SDL: Invalid buffer size: %i (should be %i), skipping..\n", length >> 2, audioDriver->periodSize);
}
// See comment in AudioDriver_ALSA.cpp
else
{
audioDriver->fillAudioWithCompensation((char*)stream, length);
}
}

AudioDriver_SDL::AudioDriver_SDL() :
AudioDriver_COMPENSATE()
AudioDriver_COMPENSATE()
{
}

Expand All @@ -70,93 +70,93 @@ AudioDriver_SDL::~AudioDriver_SDL()
// otherwise return the number of 16 bit words contained in the obtained buffer
mp_sint32 AudioDriver_SDL::initDevice(mp_sint32 bufferSizeInWords, mp_uint32 mixFrequency, MasterMixer* mixer)
{
SDL_AudioSpec wanted, obtained, saved;
char name[32];
mp_sint32 res = AudioDriverBase::initDevice(bufferSizeInWords, mixFrequency, mixer);
if (res < 0)
{
return res;
}

wanted.freq = mixFrequency;
wanted.format = AUDIO_S16SYS;
wanted.channels = 2; /* 1 = mono, 2 = stereo */
wanted.samples = bufferSizeInWords / wanted.channels; /* Good low-latency value for callback */

wanted.callback = fill_audio;
wanted.userdata = (void*)this;

mp_sint32 finalWantedSize = wanted.samples * wanted.channels;

// Some soundcard drivers modify the wanted structure, so we copy it here
memcpy(&saved, &wanted, sizeof(wanted));

if(SDL_OpenAudio(&wanted, &obtained) < 0)
{
memcpy(&wanted, &saved, sizeof(wanted));
fprintf(stderr, "SDL: Failed to open audio device! (buffer = %d bytes)..\n", saved.samples*4);
fprintf(stderr, "SDL: Try setting \"Force 2^n sizes\" in the config menu and restarting.\n");
return MP_DEVICE_ERROR;
}

printf("SDL: Using audio driver: %s\n", SDL_AudioDriverName(name, 32));

if(wanted.format != obtained.format)
{
fprintf(stderr, "SDL: Audio driver doesn't support 16-bit signed samples!\n");
return MP_DEVICE_ERROR;
}

if (wanted.channels != obtained.channels)
{
fprintf(stderr, "SDL: Failed to obtain requested audio format. Suggested format:\n");
fprintf(stderr, "SDL: Frequency: %d\nChannels: %d\n", obtained.freq, obtained.channels);
return MP_DEVICE_ERROR;
}

// fallback for obtained sample rate
if (wanted.freq != obtained.freq)
{
this->mixFrequency = obtained.freq;
}

printf("SDL: Buffer size = %i samples (requested %i)\n", obtained.samples, finalWantedSize / wanted.channels);

periodSize = obtained.samples;
// If we got what we requested, return MP_OK,
// otherwise return the actual number of samples * number of channels
return (bufferSizeInWords / wanted.channels == obtained.samples) ? MP_OK : obtained.samples * obtained.channels;
SDL_AudioSpec wanted, obtained, saved;
char name[32];
mp_sint32 res = AudioDriverBase::initDevice(bufferSizeInWords, mixFrequency, mixer);
if (res < 0)
{
return res;
}

wanted.freq = mixFrequency;
wanted.format = AUDIO_S16SYS;
wanted.channels = 2; /* 1 = mono, 2 = stereo */
wanted.samples = bufferSizeInWords / wanted.channels; /* Good low-latency value for callback */

wanted.callback = fill_audio;
wanted.userdata = (void*)this;

mp_sint32 finalWantedSize = wanted.samples * wanted.channels;

// Some soundcard drivers modify the wanted structure, so we copy it here
memcpy(&saved, &wanted, sizeof(wanted));

if(SDL_OpenAudio(&wanted, &obtained) < 0)
{
memcpy(&wanted, &saved, sizeof(wanted));
fprintf(stderr, "SDL: Failed to open audio device! (buffer = %d bytes)..\n", saved.samples*4);
fprintf(stderr, "SDL: Try setting \"Force 2^n sizes\" in the config menu and restarting.\n");
return MP_DEVICE_ERROR;
}

printf("SDL: Using audio driver: %s\n", SDL_GetCurrentAudioDriver());

if(wanted.format != obtained.format)
{
fprintf(stderr, "SDL: Audio driver doesn't support 16-bit signed samples!\n");
return MP_DEVICE_ERROR;
}

if (wanted.channels != obtained.channels)
{
fprintf(stderr, "SDL: Failed to obtain requested audio format. Suggested format:\n");
fprintf(stderr, "SDL: Frequency: %d\nChannels: %d\n", obtained.freq, obtained.channels);
return MP_DEVICE_ERROR;
}

// fallback for obtained sample rate
if (wanted.freq != obtained.freq)
{
this->mixFrequency = obtained.freq;
}

printf("SDL: Buffer size = %i samples (requested %i)\n", obtained.samples, finalWantedSize / wanted.channels);

periodSize = obtained.samples;
// If we got what we requested, return MP_OK,
// otherwise return the actual number of samples * number of channels
return (bufferSizeInWords / wanted.channels == obtained.samples) ? MP_OK : obtained.samples * obtained.channels;
}

mp_sint32 AudioDriver_SDL::stop()
{
SDL_PauseAudio(1);
deviceHasStarted = false;
return MP_OK;
SDL_PauseAudio(1);
deviceHasStarted = false;
return MP_OK;
}

mp_sint32 AudioDriver_SDL::closeDevice()
{
SDL_CloseAudio();
deviceHasStarted = false;
return MP_OK;
SDL_CloseAudio();
deviceHasStarted = false;
return MP_OK;
}

mp_sint32 AudioDriver_SDL::start()
{
SDL_PauseAudio(0);
deviceHasStarted = true;
return MP_OK;
SDL_PauseAudio(0);
deviceHasStarted = true;
return MP_OK;
}

mp_sint32 AudioDriver_SDL::pause()
{
SDL_PauseAudio(1);
return MP_OK;
SDL_PauseAudio(1);
return MP_OK;
}

mp_sint32 AudioDriver_SDL::resume()
{
SDL_PauseAudio(0);
return MP_OK;
SDL_PauseAudio(0);
return MP_OK;
}
14 changes: 7 additions & 7 deletions src/milkyplay/drivers/sdl/AudioDriver_SDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ class AudioDriver_SDL : public AudioDriver_COMPENSATE

virtual ~AudioDriver_SDL();

virtual mp_sint32 initDevice(mp_sint32 bufferSizeInWords, mp_uint32 mixFrequency, MasterMixer* mixer);
virtual mp_sint32 closeDevice();
virtual mp_sint32 initDevice(mp_sint32 bufferSizeInWords, mp_uint32 mixFrequency, MasterMixer* mixer);
virtual mp_sint32 closeDevice();

virtual mp_sint32 start();
virtual mp_sint32 stop();
virtual mp_sint32 start();
virtual mp_sint32 stop();

virtual mp_sint32 pause();
virtual mp_sint32 resume();
virtual mp_sint32 pause();
virtual mp_sint32 resume();

virtual const char* getDriverID() { return "SDLAudio"; }
virtual const char* getDriverID() { return "SDLAudio"; }
virtual mp_sint32 getPreferredBufferSize() const { return 2048; }
};

Expand Down
8 changes: 5 additions & 3 deletions src/ppui/osinterface/sdl/SDL_ModalLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ppui/osinterface/sdl/SDL_ModalLoop.cpp
*
* Copyright 2009 Peter Barth
* Copyright 2009 Peter Barth, Dale Whinham
*
* This file is part of Milkytracker.
*
Expand All @@ -26,6 +26,8 @@
*
* Created by Peter Barth on 29.03.06.
*
* 12/5/14 - Dale Whinham
* - Port to SDL2
*/

#include <SDL.h>
Expand Down Expand Up @@ -100,9 +102,9 @@ PPModalDialog::ReturnCodes SDL_runModalLoop(PPScreen* screen, PPDialogBase* dial
// ignore old mouse motion events in the event queue
SDL_Event new_event;

if (SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEMOTION)) > 0)
if (SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0)
{
while (SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEMOTION)) > 0);
while (SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0);
processSDLEvents(new_event);
}
else
Expand Down
Loading

0 comments on commit 12cce1d

Please sign in to comment.