How to play two frequencies at the same time? - python

So basically, I want to play two frequencies on top of each other. For example, 5000Hz and 10000Hz played simultaneously on top of each other at same time. So, I used winsound in Python and pygame library, but the sounds are playing one after another and I am getting this error:
Traceback (most recent call last):
File "xyz", line 14, in <module>
kk = mixer.Sound(Snd(5000, 1))
TypeError: Unrecognized argument (type NoneType)
I wrote this code:
import pygame
from pygame import mixer
import winsound
pygame.init()
def Snd(freq, dur):
return winsound.Beep(freq, 1000*dur)
ss = Snd(5000, 1)
s = Snd(10000, 1)
kk = mixer.Sound(ss)
kk.play(-1)
k = mixer.Sound(s)
k.play(-1)
I even tried this without the variables ss and s.

Related

Calling MATLAB function from Python raises error 'undefined function'

I'm trying to call a MATLAB function from python and continue with it's result in python. My script is as simple as this:
import librosa
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')
audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = matlab.double(audio.tolist())
choi_williams = eng.dtfd_nonsep(audio ,'cw',{30})
print(choi_williams)
I'm creating a linear chirp and expect to calculate it's time frequency distribution with the help of the fast_TFDs libary. The fast_TFDs folder is part of the current directory I'm executing the script from. But I'm recieving this error message:
Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.
Traceback (most recent call last): File "cw.py", line 12, in
choi_williams = eng.dtfd_nonsep(audio ,'cw',{30}) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/matlabengine.py",
line 70, in call
return FutureResult(self._engine(), future, nargs, _stdout, File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/futureresult.py",
line 67, in result
return self.__future.result(timeout) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/fevalfuture.py",
line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None,
out=self._out, err=self._err) matlab.engine.MatlabExecutionError:
Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.#
So what am I doing wrong? I'm not even sure whether the issue is the input arguments or that the function cannot be found. Is the use of the function wrong? Maybe even the import of them? Or is there a problem with the input parameters? Anyone got a clue?
The solution was as wwweagle suggested: Simply add cell2mat an it works:
import librosa
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')
audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = eng.cell2mat(audio.tolist())
audio = eng.double(audio)
choi_williams = eng.dtfd_nonsep(audio, 'cw', {eng.double(30)})
print(choi_williams)

Type Error: record() missing 1 required positional argument: 'source'

I have just started with python and i'm trying out new modules from random tutorials online.
I tried to write it exactly like the tutorial and even changing the name but it still keeps showing me this:
Traceback (most recent call last):
File "C:/Users/Asjad/PycharmProjects/untitled/hello world.py", line 7, in <module>
audio = r.record(j)
TypeError: record() missing 1 required positional argument: 'source'
I don't know how to go past this as this a new module and a new error with me
mainscript:
import speech_recognition as sy
file = ("D:\\Audio 1.wav")
r = sy.Recognizer # initialize the recognizer
with sy.AudioFile(file) as j:
audio = r.record(j)
print('it contains', r.recognise_google(audio))
I think this line r = sy.Recognizermake an instance of Recognizer so you should do that instead r = sy.Recognizer()

Audio isn't loading

I have used pygame a lot, but never used sound before. I have checked and the files are there by the right name, and I have used the right pathway, but I just get this error message:
Traceback (most recent call last):
File "C:\Users\Rajive\AppData\Local\Programs\Python\Python3.4.3\D C W.py", line 11, in module
door_opening = pygame.mixer.Sound('C:\Users\Rajive\Downloads\door_opening.m4a')
pygame.error: Unable to open file 'C:\Users\Rajive\Downloads\door_opening.m4a'
import random, time, pygame
time = time.clock()
pygame.init()
diff = 1
door_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\door_opening.m4a')
door_closing = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\door_closing.m4a')
closet_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\closet_opening.m4a')
closet_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\closet_closing.m4a')
window_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\window_opening.m4a')
window_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\window_closing.m4a')
From pygame documentation:
The Sound can be loaded from an OGG audio file or from an uncompressed WAV.
Doesn't look like the .m4a codec is supported. Perhaps try converting your file first.

TypeError: object() takes no parameters (trex dino bot)

how can i solve this problem please tell me what to do
this i am not able to figure out this problem. so please do tell me how to solve this in simple words.
import time
from PIL import ImageGrab
import pyautogui
class Cordinates():
replayBtn = (340,420)
dinosaur = (422,170)
#215= xcordinate to check for tree
def restartGame():
pyautogui.click(Cordinates.replayBtn)
def pressSapce():
pyautogui.keyDown('space')
time.sleep(0.05)
print("Jump")
pyautogui.KeyUp('space')
restartGame()
time.sleep(1)
pressSapce()
after runing i get
C:\Users\Pranav\PycharmProjects\untitled1\venv\Scripts\python.exe
C:/Users/Pranav/PycharmProjects/untitled1/bot.py
Traceback (most recent call last):
Jump
File "C:/Users/Pranav/PycharmProjects/untitled1/bot.py", line 22, in <module>
pressSapce()
File "C:/Users/Pranav/PycharmProjects/untitled1/bot.py", line 18, in pressSapce
pyautogui.KeyUp('space')
TypeError: object() takes no parameters
Process finished with exit code 1
how to solve :-
pyautogui.KeyUp('space')
TypeError: object() takes no parameters
what do you mean by the above message
you need to use keyUp not KeyUp with capital letter docs
pyautogui.keyUp('space')
pyautogui.KeyUp('space')
is supposed to be
pyautogui.keyUp('space')
That should fix your problem.

How to share string between processes

I have the hardest time to share a string between processes. I looked at the following Q1, Q2, Q3 but still fail in an actual example. If I understood the docs correctly, looking should be done automatically therefore there should be no race condition with setting/reading. Here's what I've got:
#!/usr/bin/env python3
import multiprocessing
import ctypes
import time
SLEEP = 0.1
CYCLES = 20
def child_process_fun(share):
for i in range(CYCLES):
time.sleep(SLEEP)
share.value = str(time.time())
if __name__ == '__main__':
share = multiprocessing.Value(ctypes.c_wchar_p, '')
process = multiprocessing.Process(target=child_process_fun, args=(share,))
process.start()
for i in range(CYCLES):
time.sleep(SLEEP)
print(share.value)
which produces:
Traceback (most recent call last):
File "test2.py", line 23, in <module>
print(share.value)
File "<string>", line 5, in getvalue
ValueError: character U+e479b7b0 is not in range [U+0000; U+10ffff]
Edit: ´id(share.value)´ is different for each process. However if I try a double as shared variable instead they are the same and it works like a charm. Could this be a python bug?

Categories

Resources