Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Audio recording in parallel with an RSVP task

Hello everyone! In my current experiment, I have an RSVP task and I want to synchronously record the voice of the participants. (There is no specific duration in the RSVP task, as there is a random number of elements in every trial).

I have written the following code but I have two main problems:


1_ I cannot synchronously have the audio recording and RSVP inline scripts. Either the recording happens before, or after the RSVP task.


2_ If i don't define the seconds for the audiorecording, I'm having wav. files of zero duration. However, I don't want the recording to be time specific, but rather lasts as long as the RSVP does. That is why I am using the example of seconds = 3. However, it suceeds or preceeds the RSVP script

import wave

import os
import random



chunk = 1024 # Record in chunks of 1024 samples

sample_format = pyaudio.paInt16 # 16 bits per sample

channels = 1

seconds = 3

fs = 44100 # Record at 44100 samples per second

output_folder = 'C:\\Users\\vassilis\\Desktop\\test_file'

filename = os.path.join(output_folder, f"{trial_count}.wav")


p = pyaudio.PyAudio() # Create an interface to PortAudio


# Open the mic

stream = p.open(format=sample_format,

channels=channels,

rate=fs,

frames_per_buffer=chunk,

input=True)

frames = [] # Initialize array to store frames


# Store data in chunks for 3 seconds

for i in range(0, int(fs / chunk * seconds)):

data = stream.read(chunk)

frames.append(data)

# ################################################
# THE RSVP TASK


var.T_color = "rgb(255, 255, 255)"

var.letter_dur = 15

var.isi = random.randint(75,86)




var.T_pos = random.randint(7,15)

var.max_lag = 8

var.stream_len = var.T_pos + var.max_lag + 1




letter = ["Α","Β","Γ","Δ","Ε","Ζ","Η","Θ","Ι","Κ","Λ","Μ","Ν","Ξ","Ο","Π","Ρ","Σ","Τ","Υ","Φ","Χ","Ψ","Ω"]

stim_list = random.sample(letter, var.stream_len)




letter_canvas_list = []




for i, stim in enumerate(stim_list):

letter_canvas = Canvas()

if i == var.T_pos:

letter_canvas.color=u'rgb(255, 255, 255)'




letter_canvas.text(stim)

letter_canvas_list.append(letter_canvas)

black_canvas = Canvas()

var.T = stim_list[var.T_pos]

for letter_canvas in letter_canvas_list:

letter_canvas.show()

clock.sleep(var.letter_dur)

black_canvas.show()

clock.sleep(var.isi)

# ########################################################



# Stop and close the stream

stream.stop_stream()

stream.close()

# Terminate the PortAudio interface

p.terminate()




# Save the recorded data as a WAV file

wf = wave.open(filename, 'wb')

wf.setnchannels(channels)

wf.setsampwidth(p.get_sample_size(sample_format))

wf.setframerate(fs)

wf.writeframes(b''.join(frames))

wf.close()

Comments

  • Hi Eliza,

    2_ If i don't define the seconds for the audiorecording, I'm having wav. files of zero duration. However, I don't want the recording to be time specific, but rather lasts as long as the RSVP does. That is why I am using the example of seconds = 3. However, it suceeds or preceeds the RSVP script

    Yeah, the only way to go about this is to integrate the RSVP into the recording. So, start the recording, start the RSVP, and then stop the recording. Even if you don't know the length of the RSVP, you can provide a maximum value, right?


    1_ I cannot synchronously have the audio recording and RSVP inline scripts. Either the recording happens before, or after the RSVP task.

    I am not sure because your code is not correctly formatted (no indents!), but I think it should be possible to wrap the RSVP into the loop of the stream reader. Not ideal, but possible. Potentially it might even work to run a couple of those loops during the RSVP, but again, I would need to see the code to be (slightly more) certain and you definitely would need to check the timing to be sure.

    Eduard

    Buy Me A Coffee

  • Thank you Eduard for your immediate response. I send you the code with the indents. I also have left an example of 3 seconds for recording, however I cannot insert the rsvp task inside the recording script, thus the recording happens either before or after the RSVP task.

    import pyaudio
    import wave
    import os
    import random
    
    chunk = 1024 # Record in chunks of 1024 samples
    sample_format = pyaudio.paInt16 # 16 bits per sample
    channels = 1
    seconds = 3
    fs = 44100 # Record at 44100 samples per second
    output_folder = 'C:\\Users\\vassilis\\Desktop\\test_file' #εδω βαλε το δικο σου path
    filename = os.path.join(output_folder, f"{trial_count}.wav")
    
    p = pyaudio.PyAudio() # Create an interface to PortAudio
    
    
    stream = p.open(format=sample_format,
        channels=channels,
        rate=fs,
        frames_per_buffer=chunk,
        input=)
    
    frames = [] # Initialize array to store frames
    
    for i in range(0, int(fs / chunk * seconds)):
        data = stream.read(chunk)
        frames.append(data)
    
    
    # #######################################
    
    # RSVP task
    
    var.T_color = "rgb(255, 255, 255)"
    var.letter_dur = 15
    var.isi = random.randint(75,86)
    
    var.T_pos = random.randint(7,15)
    var.max_lag = 8
    var.stream_len = var.T_pos + var.max_lag + 1
    
    letter = ["Α","Β","Γ","Δ","Ε","Ζ","Η","Θ","Ι","Κ","Λ","Μ","Ν","Ξ","Ο","Π","Ρ","Σ","Τ","Υ","Φ","Χ","Ψ","Ω"]
    stim_list = random.sample(letter, var.stream_len)
    
    letter_canvas_list = []
    
    for i, stim in enumerate(stim_list):
        letter_canvas = Canvas()
        if i == var.T_pos:
            letter_canvas.color=u'rgb(255, 255, 255)'
    
        letter_canvas.text(stim)
        letter_canvas_list.append(letter_canvas)
    
    black_canvas = Canvas()
    
    var.T = stim_list[var.T_pos]
    
    for letter_canvas in letter_canvas_list:
        letter_canvas.show()
        clock.sleep(var.letter_dur)
        black_canvas.show()
        clock.sleep(var.isi)
    
    
    stream.stop_stream()
    stream.close()
    p.terminate()
    
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()
    
    


  • Hi Eliza,

    I can't get my pyaudio installation to work properly (with the limited amount of time that I have), so I can't really try out and advice things. However, what I had in mind is something like in the second code snippet here. In the while loop stream.is_active() you could present the individual elements from the rsvp, something like:

    while stream.is_active():
         canvas_list.pop(0).show()
         clock.sleep(50) 
    

    You know what I mean? Not sure whether it will work like that, but it looks promising to me. That being said, I would recommend you draw every frame in your rsvp on a separate canvas, prepare everything in the prepare phase and add the finished canvasses to a list with all the canvases. So that the processing demands are minimal in the run phase (show is very quick compared to drawing things).

    I hope this helps,

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games