sox create and play tone only on left or right channel - python

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!

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

Related

MPI execution not as expectation

I am doing the project on Raspberry Pi 400 with Raspberry Pi OS 64-bit version. I have install the mpich using sudo apt install mpich, and I also installed mpi4py using pip. When I run the test program with the following code:
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
data = [(x+1)**x for x in range(size)]
print('we will be scattering:',data)
else:
data = None
data = comm.scatter(data, root=0)
print('rank',rank,'has data:',data)
I run it with mpiexec -n 3 python test.py but it results as following:
pi#raspberrypi:~/FYP $ mpiexec -n 3 python test.py
we will be scattering: [1]
rank 0 has data: 1
we will be scattering: [1]
rank 0 has data: 1
we will be scattering: [1]
rank 0 has data: 1
I expected that the mpiexec will be run with different ranks recieve the data from rank 0. What happen to the setting of the MPI?

How do I list all files that are older than "X" hours in Centos7 using python/bash script? [duplicate]

This question already has an answer here:
find -mtime files older than 1 hour [duplicate]
(1 answer)
Closed 2 years ago.
How can I list all files that are older than "X" hours using a python script or bash script in Centos7?
Many thx!
You can use the GNU find command:
find [path] -mmin +[minutes]
Example: find all the files in / older than 2 hours:
find / -mmin +120
you can use find command :
find <path> <conditions> <actions>
File type condation:
-type f # File
-type d # Directory
-type l # Symlink
Access time conditions :
-atime 0 # Last accessed between now and 24 hours ago
-atime +0 # Accessed more than 24 hours ago
-atime 1 # Accessed between 24 and 48 hours ago
-atime +1 # Accessed more than 48 hours ago
-atime -1 # Accessed less than 24 hours ago (same a 0)
-ctime -6h30m # File status changed within the last 6 hours and 30 minutes
-mtime +1w # Last modified more than 1 week ago
Actions :
-exec rm {} \;
-print
-delete
Also you can deal with size :
-size 8 # Exactly 8 512-bit blocks
-size -128c # Smaller than 128 bytes
-size 1440k # Exactly 1440KiB
-size +10M # Larger than 10MiB
-size +2G # Larger than 2GiB
Examples:
find . -type f ctime -6h30m # File status changed within the last 6 hours and 30 minutes
find . -type f -mtime +29 # find files modified more than 30 days ago

Guarantee that a C program starting at exactly the same time as a Python program will have the same integer time value

I am currently writing code involving piping data from a C program to a Python program. This requires that they both have exactly the same time value as an integer. My method of getting the time is:
time(0) for C
int(time.time()) for Python
However, I am getting inconsistencies in output leading me to believe that this is not resulting in the same value. The C program takes < 0.001s to run, while:
time ./cprog | python pythonprog.py
gives times typically looking like this:
real 0m0.043s
user 0m0.052s
sys 0m0.149s
Approximately one in every 5 runs results in the expected output. Can I make this more consistent?
Not a solution - but an explanation.
When starting python (or other interpreted/VM langauge), there is usually startup cost associated with read and parsing the many modules that are needed. Even a small Python program like 'print 5' will will perform large number of IO.
The startup cost will delay the initial lookup for the current time.
From strace output, invoking a python code will result in >200 open calls, ~ (f)stat, >20 mmap calls, etc.
strace -c python prog.py
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
31.34 0.000293 1 244 178 openat
9.84 0.000092 1 100 fstat
9.20 0.000086 1 90 60 stat
8.98 0.000084 1 68 rt_sigaction
7.81 0.000073 1 66 close
2.14 0.000020 2 9 brk
1.82 0.000017 9 2 munmap
1.39 0.000013 1 26 mmap
1.18 0.000011 2 5 lstat
...

Listing all usb mass storage disks using python

I have few USB disks inserted in my system. I would like to list all of them like:-
/dev/sdb
/dev/sdc
.... ans so on..
Please remember that I don't want to list partitions in it like /dev/sdb1. I am looking for solution under Linux. Tried cat /proc/partitions.
major minor #blocks name
8 0 488386584 sda
8 1 52428800 sda1
8 2 52428711 sda2
8 3 1 sda3
8 5 52428800 sda5
8 6 15728516 sda6
8 7 157683712 sda7
8 8 157682688 sda8
11 0 1074400 sr0
11 1 47602 sr1
8 32 3778852 sdc
8 33 1 sdc1
8 37 3773440 sdc5
But it list all the disks and unable to figure which one is USB storage disks. I am looking for a solution which does not require an additional package installation.
You can convert Klaus D.'s suggestion into Python code like this:
#!/usr/bin/env python
import os
basedir = '/dev/disk/by-path/'
print 'All USB disks'
for d in os.listdir(basedir):
#Only show usb disks and not partitions
if 'usb' in d and 'part' not in d:
path = os.path.join(basedir, d)
link = os.readlink(path)
print '/dev/' + os.path.basename(link)
path contains info in this format:
/dev/disk/by-path/pci-0000:00:1d.7-usb-0:5:1.0-scsi-0:0:0:0
which is a symbolic link, so we can get the pseudo-scsi device name using os.readlink().
But that returns info in this format:
../../sdc
so we use os.path.basename() to clean it up.
Instead of using
'/dev/' + os.path.basename(link)
you can produce a string in the '/dev/sdc' format by using
os.path.normpath(os.path.join(os.path.dirname(path), link))
but I think you'll agree that the former technique is simpler. :)
List the right path in /dev:
ls -l /dev/disk/by-path/*-usb-* | fgrep -v part

heapy reports memory usage << top

NB: This is my first foray into memory profiling with Python, so perhaps I'm asking the wrong question here. Advice re improving the question appreciated.
I'm working on some code where I need to store a few million small strings in a set. This, according to top, is using ~3x the amount of memory reported by heapy. I'm not clear what all this extra memory is used for and how I can go about figuring out whether I can - and if so how to - reduce the footprint.
memtest.py:
from guppy import hpy
import gc
hp = hpy()
# do setup here - open files & init the class that holds the data
print 'gc', gc.collect()
hp.setrelheap()
raw_input('relheap set - enter to continue') # top shows 14MB resident for python
# load data from files into the class
print 'gc', gc.collect()
h = hp.heap()
print h
raw_input('enter to quit') # top shows 743MB resident for python
The output is:
$ python memtest.py
gc 5
relheap set - enter to continue
gc 2
Partition of a set of 3197065 objects. Total size = 263570944 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 3197061 100 263570168 100 263570168 100 str
1 1 0 448 0 263570616 100 types.FrameType
2 1 0 280 0 263570896 100 dict (no owner)
3 1 0 24 0 263570920 100 float
4 1 0 24 0 263570944 100 int
So in summary, heapy shows 264MB while top shows 743MB. What's using the extra 500MB?
Update:
I'm running 64 bit python on Ubuntu 12.04 in VirtualBox in Windows 7.
I installed guppy as per the answer here:
sudo pip install https://guppy-pe.svn.sourceforge.net/svnroot/guppy-pe/trunk/guppy

Categories

Resources