2

I am trying to create a tone in python and play it only on either the left channel or right channel. I can't figure out how to specify playing on the chosen channel.

Here is my code so far:

import os

frequency = 1000 #hz

duration = 2000 #milliseconds

os.system('play -n synth %s sin %s' % (duration/1000, frequency))

I have experimented with "remix" but haven't been successful.

Thanks for the help!

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
GOB
  • 143
  • 10

1 Answers1

3

I suggest getting sox to output what you want first before adding to your python code. This should do what you want (left first, then right).

play -n -c 2 synth 2 sin 1000 remix 1 0
play -n -c 2 synth 2 sin 1000 remix 0 1

It's possible you might also have an issue with the default device for sox. play is simply an alias for sox -d i.e. using the default device for output. To check this, or if you want the output to go to a file, you can use this:

sox -n -c 2 left.wav synth 2 sin 1000 remix 1 0
sox -n -c 2 right.wav synth 2 sin 1000 remix 0 1

Your python code could incorporate this like so:

import os    

frequency = 1000 #hz

duration = 2000 #milliseconds

os.system('play -n -c 2 synth %s sin %s remix 1 0' % (duration/1000, frequency))
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
  • Thank you for the answer! Is there any way to make this non-blocking, i.e. start the sound playing and do other stuff while the sound is playing, versus wait until the sound is played to start anything else? – GOB Aug 14 '20 at 16:56
  • 1
    Yes, if you use `subprocess.Popen()` instead - see https://stackoverflow.com/a/636570/12744 – Colin Pickard Aug 14 '20 at 17:37