Skip to content

Instantly share code, notes, and snippets.

@cosacog
Created November 29, 2016 12:29
Show Gist options
  • Save cosacog/e16f2d69c63751680d37c5e30cd3d4d3 to your computer and use it in GitHub Desktop.
Save cosacog/e16f2d69c63751680d37c5e30cd3d4d3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
from psychopy import visual, core, parallel
from psychopy.constants import * # things like STARTED, FINISHED
import numpy as np # whole numpy lib is available, prepend 'np.'
import os # handy system and path functions
from scipy import fromstring, int16
import wave
import sounddevice as sd
path_wav = './click2sec.wav'
wr = wave.open(path_wav, 'rb')
s_data = wr.readframes(wr.getnframes())
s_numdata = fromstring(s_data, dtype=int16)
mysnd = s_numdata[2205:] # skip the blank sound
sd.default.samplerate = 44100
wasapi_exclusive = sd.WasapiSettings(exclusive=True)
p_port = parallel.ParallelPort(address=u'0x0378')
trialClock = core.Clock()
'''
In [5]: sd.query_devices()
Out[5]:
0 Microsoft サウンド マッパー - Input, MME (2 in, 0 out)
1 リア入力ジャック (SoundMAX Inte, MME (2 in, 0 out)
2 マイク (SoundMAX Integrated Dig, MME (2 in, 0 out)
3 Microsoft サウンド マッパー - Output, MME (0 in, 2 out)
4 デジタル オーディオ (S/PDIF) (2, MME (0 in, 2 out)
5 スピーカー (SoundMAX Integrated, MME (0 in, 2 out)
6 スピーカー (2- HD Audio PCI-e A, MME (0 in, 2 out)
7 プライマリ サウンド キャプチャ ドライバー, Windows DirectSound (2 in, 0 out)
8 リア入力ジャック (SoundMAX Integrated Digital High Definition Audio), Windows DirectSound (2 in, 0 out)
9 マイク (SoundMAX Integrated Digital High Definition Audio), Windows DirectSound (2 in, 0 out)
10 プライマリ サウンド ドライバー, Windows DirectSound (0 in, 2 out)
11 デジタル オーディオ (S/PDIF) (2- HD Audio PCI-e Audio Device), Windows DirectSound (0 in, 2 out)
12 スピーカー (SoundMAX Integrated Digital High Definition Audio), Windows DirectSound (0 in, 2 out)
13 スピーカー (2- HD Audio PCI-e Audio Device), Windows DirectSound (0 in, 2 out)
* 14 HD Audio PCI-e Audio Device, ASIO (0 in, 2 out)
15 スピーカー (SoundMAX Integrated Digital High Definition Audio), Windows WASAPI (0 in, 2 out)
16 デジタル オーディオ (S/PDIF) (2- HD Audio PCI-e Audio Device), Windows WASAPI (0 in, 2 out)
17 スピーカー (2- HD Audio PCI-e Audio Device), Windows WASAPI (0 in, 2 out)
18 リア入力ジャック (SoundMAX Integrated Digital High Definition Audio), Windows WASAPI (2 in, 0 out)
19 マイク (SoundMAX Integrated Digital High Definition Audio), Windows WASAPI (2 in, 0 out)
20 マイク (SoundMAX HD Audio I), Windows WDM-KS (2 in, 0 out)
21 リア入力ジャック (SoundMAX HD Audio I), Windows WDM-KS (2 in, 0 out)
22 Speakers (SoundMAX HD Audio O), Windows WDM-KS (0 in, 2 out)
23 Speakers (HD Audio Speaker), Windows WDM-KS (0 in, 2 out)
24 SPDIF Out (HD Audio SPDIF out), Windows WDM-KS (0 in, 2 out)
'''
sd.default.device = [20,23] #[1,6] mme, [18,17] wasapi [20,23]wdm-ks [14,14]
# Remove comment when open a window
# win = visual.Window(size=(1024, 768), fullscr=False, screen=0, allowGUI=False, allowStencil=False,
# monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
# blendMode='avg', useFBO=True,
# )
for ii in np.arange(50):
t = 0
trialClock.reset() # clock
continueRoutine = True
p_port.status = NOT_STARTED
while continueRoutine:
t = trialClock.getTime()
if t>=1.0 and p_port.status == NOT_STARTED:
p_port.setData(int(1))
sd.play(mysnd)
# sd.play(mysnd, extra_settings=wasapi_exclusive) # wasapi exclusive mode
p_port.status = STARTED
if t>=1.1 and p_port.status == STARTED:
p_port.setData(int(0))
p_port.status = STOPPED
if t>=4.0:
continueRoutine = False
# win.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment