17

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.

2
  • What does aplay do?
    – mmmmmm
    Dec 12, 2012 at 11:34
  • See edit, linked url.
    – Michal
    Dec 12, 2012 at 12:17

8 Answers 8

9

I believe the sox utility stands in for aplay.

12

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. 

1
  • 3
    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, 2017 at 10:31
7


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.

5

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).)

3
  • 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, 2012 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, 2012 at 14:32
  • So what would you suggest?
    – Michal
    Dec 12, 2012 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 -
1
  • 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, 2018 at 11:36
2

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

1

I already had ffplay installed as part of ffmpeg, so this was what worked most easily for me:

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

See https://stackoverflow.com/questions/28174639/how-can-i-play-raw-pcm-file-that-i-dumped-from-a-codec

1

macOS includes a command named afplay, which plays an audio file to the default audio output.

To play a file, simply run:

afplay <audiofile>

You must log in to answer this question.

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