11

Anybody knows any Mac alternative for Linux's APLAY program? I would like to run this script:

echo "main(i){for(i=0;;i++)putchar(((i*(i>>8|i>>9)&46&i>>8))^(i&i>>13|i>>6));}" | gcc -x c - && ./a.out | aplay

Which makes great sound effect, but I don't know how, since Mac OS doesn't have aplay.

EDIT:

Link to Wikipedia, which describes the functionality of aplay.

  • What does aplay do? – user151019 Dec 12 '12 at 11:34
  • See edit, linked url. – Michal Dec 12 '12 at 12:17
6

I believe the sox utility stands in for aplay.

7

Here is the same program, as a perl one-liner, which works (tested) on Mac, Linux, and Windows

perl -e 'for($i=0;;$i++){
    print pack("n", ((($i*($i>>8|$i>>9)&46&$i>>8))^($i&$i>>13|$i>>6))); 
}' | ./play -c 1 -b 8 -e unsigned -t raw -r 8k -

the "./play" command is the "sox" utility already mentioned in this article. 

  • 2
    aplay uses 8khz not 48... so the command should rather be play -c 1 -b 8 -e unsigned -t raw -r 8k - – hashier Aug 19 '17 at 10:31
5


I know that this topic is old, but I've find a recent working solution. SOX doesn't support input from stdout anymore so now a working alternative is mplayer. You'll need to install it using Hombrew.sh with brew install mplayer.

You can then replace aplay by: mplayer -cache 1024 -quiet -rawaudio samplesize=1:channels=1:rate=8000 -demuxer rawaudio -

Just be sure to change rate=8000 with your custom frequency.

You can create an alias in ~/.bashrc for aplay to pipe your PCM chiptune in like this:

alias aplay="mplayer -cache 1024 -quiet -rawaudio samplesize=1:channels=1:rate=8000 -demuxer rawaudio -"

Hope this helped, tested on macOs Sierra 10.12.5 today.

3

aplay needs ALSA which is only available on Linux. You could try outputting that bytestream to a file and try to play with afplay on OS X. (You likely need to add proper AIFF headers for afplay to accept the file since it won't play a raw PCM dump (or however that bytestream coming out of that echo should be interpreted).)

  • The fun part about the aplay and this little C algorithm is that it changes and generates different sounds over time, so outputting it would take that away :( – Michal Dec 12 '12 at 14:02
  • See it as a longer time delay for buffering playback. :-) how is that data to be interpreted? That might help to find a suitable command line utility that works on OS X to play the data generated by the utility. (For example that might work with mpg123 though I haven't tried that yet.) – MacLemon Dec 12 '12 at 14:32
  • So what would you suggest? – Michal Dec 12 '12 at 16:08
2

I used mpv. The options are similar to mplayer, but the --rawaudio switch is no longer supported in mpv. This is what you do instead:

echo 'c_program' | gcc and crap | mpv --cache=1024 --quiet \
    --demuxer=rawaudio \
    --demuxer-rawaudio-format=u8 \
    --demuxer-rawaudio-channels=1 \
    --demuxer-rawaudio-rate=8000 -
  • I got a bit confused with "gcc and crap", but I figured out that you were just referring to the params passed to the compiler. Works really well, cheers! – Michal Jan 20 '18 at 11:36
1

Install sox from Homebrew, then use play -c1 -b8 -eunsigned -traw -r8k - instead of auplay.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .