Subject: uade 1.02 for Mac OS X (Darwin)

From: MB
Date: Wed May 11 22:16:07 2005

To: UADE-Developer-discussion
Hi, I've put a little effort into making uade 1.02 compile nicely on Mac OS X. I've modified Stuart Caie's Mac OS X coreaudio patch to work with 1.02, and I've also updated the configure system to use glibtool.

Here are the details.

Here is the new complete INSTALL.MacOSX:
[code:1:f4e892982e]
Small instructions to compile UADE &amp; XMMS-plugin on Mac OS X &#40;Darwin&#41;.

- Command line player only
---------------------------

To compile only the uade command line player&#58;
./configure --without-xmms; make; sudo make install

- XMMS plugin
-------------

Requirements&#58;
Fink - http&#58;//fink.sf.net
  &#40;and via fink&#41;
  - xmms
  - gnu libtool

Installation steps&#58;
1&#41; fink install xmms
2&#41; fink install libtool14
&#40;in uade directory&#41;
3&#41; ./configure; make; make install

&#40;thanks to Michael Baltaks for this recipy &#58;&#41;
[/code:1:f4e892982e]

Here is the configure diff.
[code:1:f4e892982e]
--- uade-1.02/configure	Fri May  6 05&#58;53&#58;00 2005
+++ uade-1.02_coreaudio/configure	Wed May 11 21&#58;31&#58;41 2005
@@ -244,6 +244,7 @@
 AUDIOTARGET=&quot;NULL&quot;
 OSSOURCE=&quot;uade-os-unix.c&quot;
 OSHEADER=&quot;uade-os-unix.h&quot;
+LIBTOOL=libtool
 
 if test -n &quot;$OS&quot; &amp;&amp; test &quot;$OS&quot; = &quot;SunOS&quot;; then
     echo
@@ -265,10 +266,10 @@
 elif test -n &quot;$OS&quot; &amp;&amp; test &quot;$OS&quot; = &quot;Darwin&quot;; then
     echo
     echo &quot;Configuring for MacOSX&quot;
-    AUDIOTARGET=&quot;SDL&quot;
+    AUDIOTARGET=&quot;COREAUDIO&quot;
     ARCHFLAGS=&quot;$ARCHFLAGS -no-cpp-precomp&quot;
-    ARCHLIBS=&quot;$ARCHLIBS -lpoll&quot;
     echo &quot;#define HAVE_MACOSX&quot; &gt;&gt; config.h
+    LIBTOOL=glibtool
 elif test -n &quot;$OS&quot; &amp;&amp; test &quot;$OS&quot; = &quot;Linux&quot;; then
     if test &quot;$interaction&quot; != &quot;nooo&quot;; then
         interaction=&quot;yes&quot;
@@ -349,6 +350,10 @@
     fi
     SOUNDSOURCE=&quot;sd-sound-ao.c&quot;
     SOUNDHEADER=&quot;sd-sound-ao.h&quot;
+elif test &quot;$AUDIOTARGET&quot; = &quot;COREAUDIO&quot;; then
+    AUDIOLIBS=&quot;-framework coreaudio&quot;
+    SOUNDSOURCE=&quot;sd-sound-coreaudio.c&quot;
+    SOUNDHEADER=&quot;sd-sound-coreaudio.h&quot;
 else
     SOUNDSOURCE=&quot;sd-sound-null.c&quot;
     SOUNDHEADER=&quot;sd-sound-null.h&quot;
@@ -548,6 +553,7 @@
 	-e &quot;s|&#123;CC&#125;|$CC|&quot; \
 	-e &quot;s|&#123;VERSION&#125;|$VERSION|&quot; \
 	-e &quot;s|&#123;DEBUGFLAGS&#125;|$debug_flags|&quot; \
+	-e &quot;s|&#123;LIBTOOL&#125;|$LIBTOOL|&quot; \
 	plugindir/xmms/Makefile.in &gt; plugindir/xmms/Makefile
 fi
 
[/code:1:f4e892982e]

Here is the plugins/xmms/Makefile.in diff.
[code:1:f4e892982e]
--- uade-1.02/plugindir/xmms/Makefile.in	Fri May  6 05&#58;53&#58;00 2005
+++ uade-1.02_coreaudio/plugindir/xmms/Makefile.in	Wed May 11 21&#58;30&#58;36 2005
@@ -38,7 +38,7 @@
 LDFLAGS = $&#40;LIBFLAG&#41; $&#40;GLIBLIBS&#41; $&#40;XMMSLIBS&#41; $&#40;ARCHLIBS&#41;
 DECFLAGS  = -c -O2 -W -Wall -Wno-implicit-int -finline-functions $&#40;ARCHFLAGS&#41; $&#40;DEBUGFLAGS&#41;
 
-LIBTOOL = libtool
+LIBTOOL = &#123;LIBTOOL&#125;
 
 # THERE SHOULDN'T BE ANYTHING TO EDIT AFTER THIS LINE
 
[/code:1:f4e892982e]

Here is the uade-os-unix.h diff.

[code:1:f4e892982e]
--- uade-1.02/osdep/uade-os-unix.h	Fri May  6 05&#58;53&#58;00 2005
+++ uade-1.02_coreaudio/osdep/uade-os-unix.h	Wed May 11 21&#58;56&#58;03 2005
@@ -33,7 +33,7 @@
 
 #define UADE_SIGNALHANDLER&#40;x&#41; void x&#40;int signum&#41;
 
-char *uade_base_dir;
+extern char *uade_base_dir;
 
 int uade_create_signalhandler&#40;void &#40;*sighandler&#41;&#40;int&#41;, int sigtype&#41;;
 void uade_create_var_pid&#40;void&#41;;
[/code:1:f4e892982e]

Here is the header for the coreaudio support (sd-sound-coreaudio.h).

[code:1:f4e892982e]
 /* 
  * UAE - The Un*x Amiga Emulator
  * 
  * Support for Mac OS X CoreAudio
  * 
  * &#40;C&#41; 2004 Stuart Caie
  */

#include &lt;unistd.h&gt;
#include &lt;errno.h&gt;
#include &quot;uade.h&quot;

extern uae_u16 *sndbuffer, *sndbufpt;
extern int sndbufsize;
extern int sound_bytes_per_sample;
extern void finish_sound_buffer &#40;void&#41;;

static __inline__ void check_sound_buffers &#40;void&#41;
&#123;

	if &#40;&#40;char *&#41;sndbufpt - &#40;char *&#41;sndbuffer &gt;= sndbufsize&#41;
	&#123;
		if &#40;&#40;char *&#41;sndbufpt - &#40;char *&#41;sndbuffer &gt; sndbufsize&#41;
		&#123;
			fprintf&#40;stderr, &quot;uade&#58; A bug in sound buffer writing. Report this!\n&quot;&#41;;
		&#125;

		if &#40;uade_check_sound_buffers&#40;sndbuffer, sndbufsize, sound_bytes_per_sample&#41;&#41;
		&#123;
			finish_sound_buffer&#40;&#41;;
		&#125;
		sndbufpt = sndbuffer;
	&#125;
&#125;

#define PUT_SOUND_BYTE&#40;b&#41; do &#123; \
  *&#40;uae_u8 *&#41;sndbufpt = b; \
  sndbufpt = &#40;uae_u16 *&#41;&#40;&#40;&#40;uae_u8 *&#41;sndbufpt&#41; + 1&#41;; \
&#125; while &#40;0&#41;
#define PUT_SOUND_WORD&#40;b&#41; do &#123; \
  *&#40;uae_u16 *&#41;sndbufpt = b; \
  sndbufpt = &#40;uae_u16 *&#41;&#40;&#40;&#40;uae_u8 *&#41;sndbufpt&#41; + 2&#41;; \
&#125; while &#40;0&#41;
#define PUT_SOUND_BYTE_LEFT&#40;b&#41; PUT_SOUND_BYTE&#40;b&#41;
#define PUT_SOUND_WORD_LEFT&#40;b&#41; PUT_SOUND_WORD&#40;b&#41;
#define PUT_SOUND_BYTE_RIGHT&#40;b&#41; PUT_SOUND_BYTE&#40;b&#41;
#define PUT_SOUND_WORD_RIGHT&#40;b&#41; PUT_SOUND_WORD&#40;b&#41;
#define SOUND16_BASE_VAL 0
#define SOUND8_BASE_VAL 0

#define DEFAULT_SOUND_MAXB &#40;6144 * 4&#41;
#define DEFAULT_SOUND_MINB &#40;14 * 4&#41;
#define DEFAULT_SOUND_BITS 16
#define DEFAULT_SOUND_FREQ 44100
#define HAVE_STEREO_SUPPORT
[/code:1:f4e892982e]

Here is the source for coreaudio support (sd-sound-coreaudio.c).

[code:1:f4e892982e]
/* 
  * UAE - The Un*x Amiga Emulator
  * 
  * Support for Mac OS X CoreAudio
  * 
  * &#40;C&#41; 2004 Stuart Caie
  * based on the Fink esound patch by Shawn Hsiao and Masanori Sekino
  */

#include &quot;../config.h&quot;

#include &quot;sysconfig.h&quot;
#include &quot;sysdeps.h&quot;

#include &quot;config.h&quot;
#include &quot;options.h&quot;
#include &quot;memory.h&quot;
#include &quot;custom.h&quot;
#include &quot;gensound.h&quot;
#include &quot;sd-sound.h&quot;
#include &quot;uade.h&quot;

#include &lt;CoreAudio/AudioHardware.h&gt;
#include &lt;pthread.h&gt;
#include &lt;mach/mach.h&gt;
#include &lt;mach/thread_policy.h&gt;
#include &lt;sys/param.h&gt;
#include &lt;sys/sysctl.h&gt;

/* the UAE soundbuffer */
uae_u16 *sndbuffer = NULL, *sndbufpt;
int sndbufsize = 0;

int sound_bytes_per_sample;

static AudioDeviceID outputDevice;
static Float32 *outputBuf = NULL;
static int outputAvail = 0, outputSize = 0;
static pthread_mutex_t outputMutex;
static pthread_cond_t outputCond;

static OSStatus sound_callback&#40;AudioDeviceID inDevice,
  const AudioTimeStamp *inNow, const AudioBufferList *inInputData,
  const AudioTimeStamp *inInputTime, AudioBufferList *outOutputData,
  const AudioTimeStamp *inOutputTime, void *inClientData&#41;
&#123;
  Float32 *buf = outOutputData-&gt;mBuffers&#91;0&#93;.mData;
  pthread_mutex_lock&#40;&amp;outputMutex&#41;;
  memcpy&#40;&amp;buf&#91;0&#93;, &amp;outputBuf&#91;0&#93;, outputAvail * sizeof&#40;Float32&#41;&#41;;
  outputAvail = 0;
  pthread_mutex_unlock&#40;&amp;outputMutex&#41;;
  pthread_cond_signal&#40;&amp;outputCond&#41;;
  return kAudioHardwareNoError;
&#125;

void finish_sound_buffer &#40;void&#41; &#123;
  /* convert data to floats and put into buffer */
  uae_s16 *sndp16 = &#40;uae_s16 *&#41; sndbuffer;
  uae_s8  *sndp8  = &#40;uae_s8 *&#41;  sndbuffer;
  Float32 scale = 1.0 / &#40;&#40;currprefs.sound_bits == 16&#41; ? 32768.0 &#58; 128.0&#41;;
  int i;

  pthread_mutex_lock&#40;&amp;outputMutex&#41;;

  /* wait for existing buffer data to be consumed */
  while &#40;outputAvail&#41; pthread_cond_wait&#40;&amp;outputCond, &amp;outputMutex&#41;;

  /* refill buffer */
  if &#40;currprefs.stereo&#41; &#123;
    if &#40;currprefs.sound_bits == 16&#41; &#123;
      for &#40;i = 0; i &lt; outputSize; i++&#41; &#123;
        outputBuf&#91;i&#93; = *sndp16++ * scale;
      &#125;
    &#125;
    else &#123;
      for &#40;i = 0; i &lt; outputSize; i++&#41; &#123;
        outputBuf&#91;i&#93; = *sndp8++ * scale;
      &#125;
    &#125;
  &#125;
  else &#123;
    if &#40;currprefs.sound_bits == 16&#41; &#123;
      for &#40;i = 0; i &lt; outputSize; i += 2&#41; &#123;
	outputBuf&#91;i&#93; = outputBuf&#91;i+1&#93; = *sndp16++ * scale;
      &#125;
    &#125;
    else &#123;
      for &#40;i = 0; i &lt; outputSize; i += 2&#41; &#123;
	outputBuf&#91;i&#93; = outputBuf&#91;i+1&#93; = *sndp8++ * scale;
      &#125;
    &#125;
  &#125;
  outputAvail = outputSize;
  pthread_mutex_unlock&#40;&amp;outputMutex&#41;;
&#125;

/* dump any sound currently in the buffer */
void flush_sound &#40;void&#41; &#123;
  sndbufpt = sndbuffer;
&#125;

/* initialise sound device */
int init_sound&#40;void&#41; &#123;
  struct thread_time_constraint_policy ttcpolicy;
  int bus_speed, mib&#91;2&#93; = &#123; CTL_HW, HW_BUS_FREQ &#125;;
  AudioValueRange frameRange;
  UInt32 size, frames;
  
  sound_bytes_per_sample = currprefs.sound_bits / 8;

  /* get realtime priority */
  size = sizeof&#40;bus_speed&#41;;
  sysctl&#40;mib, 2, &amp;bus_speed, &amp;size, NULL, 0&#41;;
  ttcpolicy.period      = bus_speed / 120;
  ttcpolicy.computation = bus_speed / 2400;
  ttcpolicy.constraint  = bus_speed / 1200;
  ttcpolicy.preemptible = 1;
  thread_policy_set&#40;mach_thread_self&#40;&#41;, THREAD_TIME_CONSTRAINT_POLICY,
		    &#40;int *&#41; &amp;ttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT&#41;;

  /* get default output device ID */
  size = sizeof&#40;outputDevice&#41;;
  if &#40;AudioHardwareGetProperty&#40;kAudioHardwarePropertyDefaultOutputDevice,
			       &amp;size, &amp;outputDevice&#41;&#41; return 0;

  /* get minimum and maximum frame sizes */
  size = sizeof&#40;frameRange&#41;;
  if &#40;AudioDeviceGetProperty&#40;outputDevice, 0, 0,
                             kAudioDevicePropertyBufferFrameSizeRange,
                             &amp;size, &amp;frameRange&#41;&#41; return 0;

  /* Assume the user's buffer size preference is already 16 bit stereo, so
   * divide by 4 to get the number of frames &#40;a frame is one sample, regardless
   * of bit width or number of channels&#41; */
  frames = currprefs.sound_maxbsiz / 4;
  if &#40;frames &lt; &#40;UInt32&#41; frameRange.mMinimum&#41; &#123;
    frames = &#40;UInt32&#41; frameRange.mMinimum;
  &#125;
  if &#40;frames &gt; &#40;UInt32&#41; frameRange.mMaximum&#41; &#123;
    frames = &#40;UInt32&#41; frameRange.mMaximum;
  &#125;

  /* set hardware audio buffer size */
  if &#40;AudioDeviceSetProperty&#40;outputDevice, 0, 0, 0,
			     kAudioDevicePropertyBufferFrameSize,
			     sizeof&#40;frames&#41;, &amp;frames&#41;&#41; return 0;

  /* outputSize is the number of samples in our conversion buffer. This is
   * always twice the number of frames, as we are always generating stereo */
  outputSize = frames * 2;

  if &#40;currprefs.sound_bits == 16&#41; &#123;
    init_sound_table16&#40;&#41;;
    sample_handler = currprefs.stereo ? sample16s_handler &#58; sample16_handler;
    sndbufsize     = currprefs.stereo ? &#40;frames * 4&#41;      &#58; &#40;frames * 2&#41;;
  &#125;
  else &#123;
    init_sound_table8&#40;&#41;;
    sample_handler = currprefs.stereo ? sample8s_handler &#58; sample8_handler;
    sndbufsize     = currprefs.stereo ? &#40;frames * 2&#41;     &#58; &#40;frames * 1&#41;;
  &#125;
  sample_evtime = &#40;long&#41; &#40;maxhpos * maxvpos * 50&#41; / currprefs.sound_freq;

  if &#40;!&#40;sndbuffer = malloc&#40;sndbufsize&#41;&#41;&#41; return 0;
  if &#40;!&#40;outputBuf = malloc&#40;outputSize * sizeof&#40;Float32&#41;&#41;&#41;&#41; return 0;
  if &#40;pthread_mutex_init&#40;&amp;outputMutex, NULL&#41;&#41; return 0;
  if &#40;pthread_cond_init&#40;&amp;outputCond, NULL&#41;&#41; return 0;
  if &#40;AudioDeviceAddIOProc&#40;outputDevice, &amp;sound_callback, NULL&#41;&#41; return 0;
  if &#40;AudioDeviceStart&#40;outputDevice, &amp;sound_callback&#41;&#41; return 0;
  sndbufpt = sndbuffer;
  return sound_available = 1;
&#125;

/* uninitialise sound device */
void close_sound&#40;&#41; &#123;
  AudioDeviceStop&#40;outputDevice, &amp;sound_callback&#41;;
  AudioDeviceRemoveIOProc&#40;outputDevice, &amp;sound_callback&#41;;
  pthread_cond_destroy&#40;&amp;outputCond&#41;;
  pthread_mutex_destroy&#40;&amp;outputMutex&#41;;
  free&#40;outputBuf&#41;; outputBuf = NULL;
  free&#40;sndbuffer&#41;; sndbuffer = NULL;
&#125;

/* determine whether sound is available */
int setup_sound&#40;&#41; &#123;
  AudioStreamBasicDescription format;
  UInt32 size;

  if &#40;!uade_local_sound&#41; return sound_available = 1;

  /* get default output device ID */
  size = sizeof&#40;outputDevice&#41;;
  if &#40;AudioHardwareGetProperty&#40;kAudioHardwarePropertyDefaultOutputDevice,
			       &amp;size, &amp;outputDevice&#41;&#41; return 0;

  /* get properties of default output device default channel stream */
  size = sizeof&#40;format&#41;;
  if &#40;AudioDeviceGetProperty&#40;outputDevice, 0, 0,
			     kAudioDevicePropertyStreamFormat,
			     &amp;size, &amp;format&#41;&#41; return 0;

  /* We need an output device that takes lpcm float samples, and it has to
   * be at the sample rate of our output already, as we don't resample. */
  return sound_available =
    &#40; &#40;format.mFormatID == kAudioFormatLinearPCM&#41; &amp;&amp;
      &#40;format.mFormatFlags &amp; kLinearPCMFormatFlagIsFloat&#41; &amp;&amp;
      &#40;currprefs.sound_freq == &#40;&#40;int&#41; format.mSampleRate&#41;&#41; &amp;&amp;
      &#40;format.mChannelsPerFrame == 2&#41; &#41;;
&#125;
[/code:1:f4e892982e]

---

From: MB
Date: Wed May 11 22:42:03 2005

To: UADE-Developer-discussion
I neglected to mention that coreaudio is the Mac OS X native audio API, and it is gives much better performance than SDL. Even on my 900MHz G3 iBook uade with SDL audio skips when doing any minor work on the computer.

I noticed that the poll dependancy has been changed to select, so I've taken that out of the docs.

I also haven't done any further work with ftw, but I might add that later.

---

From: MB
Date: Thu May 12 06:07:34 2005

To: UADE-Developer-discussion
Anyone using uade with xmms on Mac OS X would probably like to know that there is a native audio driver for xmms on OS X. This driver won't skip like the esound driver will.

[url=http://sourceforge.net/project/showfiles.php?group_id=846&amp;package_id=120865]http://sourceforge.net/project/showfiles.php?group_id=846&amp;package_id=120865[/url]

---

From: shd
Date: Thu May 12 11:00:35 2005

To: UADE-Developer-discussion
[quote:e4ae37cd22="MB"]Hi, I've put a little effort into making uade 1.02 compile nicely on Mac OS X. I've modified Stuart Caie's Mac OS X coreaudio patch to work with 1.02, and I've also updated the configure system to use glibtool.
[/quote:e4ae37cd22]

Thanks. Would you also post that as a patch to some public web page where it is easily downloadable? We should figure a way to have file attachments on this forum. Alternatively, if you will post me that patch set as one file, I can put it to http://uade.ton.tut.fi/patches/ for public view.

---

From: MB
Date: Thu May 12 23:59:57 2005

To: UADE-Developer-discussion
Here is the full patch to uade 1.02 for Darwin/Mac OS X.

[url=http://doublecommand.sourceforge.net/uade-1.02_darwin.diff]http://doublecommand.sourceforge.net/uade-1.02_darwin.diff[/url]

---

From: Guest
Date: Fri Jun 17 11:53:28 2005

To: UADE-Developer-discussion
Thank you, thank you, thank you, thanky you.

I was still using the rather outdated fink version, since never versions didn't have coreaudio support and even on my PB the sound was stuttering with SDL every now and then.

So once again, a HUGE thank you from my side,
Mike

PS: Is it possible to get this into the main source as well?

---

From: shd
Date: Fri Jun 17 12:25:21 2005

To: UADE-Developer-discussion
[quote:82323e72be="Anonymous"]
PS: Is it possible to get this into the main source as well?[/quote:82323e72be]

It is in development line already, and it has been released. See:

 http://freshmeat.net/projects/uade/?branch_id=34483

---

From: Guest
Date: Fri Jun 17 19:50:10 2005

To: UADE-Developer-discussion
Sweet, although the wife already starts complaining about "the-noise" she hears, better get my headphones. :)

Once again a BIG thank you for the program and all the effort.

Mike, a satisfied customer.