I"m trying to get Matlab to send MIDI messages to my keyboard but I am not hearing anything. I can receive into Matlab just fine using midireceive. Here's the code...
mididevinfo;
% MIDI devices available:
% ID Direction Interface Name
% 0 input CoreMIDI 'USB Keystation 88es'
% 1 output CoreMIDI 'USB Keystation 88es'
device = mididevice('USB Keystation 88es');
msgs = midimsg('Note',1,48,64,0.25);
midisend(device,msgs);
Related
I have a device that is like 3 headphones in one ( so 6 earplugs) . My goal is to play one diferent song on each earplug ( 6 songs). So I starded by playing one song on an earplug. For me one channel means one earplug (but maybe I am wrong) .I am using Psychtoolbox:
function BasicSoundOutputDemo( wavfilename)
AssertOpenGL;
% Read WAV file from filesystem:
[y, freq] = psychwavread(wavfilename);
aux = y' ;
wavedata = aux(1,:);
InitializePsychSound;
devices = PsychPortAudio('GetDevices' );
pahandle = PsychPortAudio('Open', [], [], 0, freq, 1);// nr channels = 1
PsychPortAudio('FillBuffer', pahandle, wavedata);
t1 = PsychPortAudio('Start', pahandle, 1, 0);
KbReleaseWait;
while ~KbCheck
% Wait a seconds...
WaitSecs(1);
end
PsychPortAudio('Stop', pahandle);
PsychPortAudio('Close', pahandle);
fprintf('Demo finished, bye!\n');
But it didn’t worked. Instead of playing the sound on just one earplug, it was playing on 2 earplugs.
I get this warnings
PTB-INFO: Using specially modified PortAudio engine, based on offical
version: PortAudio V19-devel WITH-DIM
Will use ASIO enhanced Portaudio driver DLL. See
Psychtoolbox/PsychSound/PortAudioLICENSE.txt for the exact terms of
use for this dll.
Disclaimer: "ASIO is a trademark and software of Steinberg Media
Technologies GmbH."
PTB-Warning: Although using the ASIO enabled Psychtoolbox sound
driver,
PTB-Warning: could not find any ASIO capable soundcard in your system.
PTB-Warning: If you think you should have an ASIO card, please check
your
PTB-Warning: system for properly installed and configured drivers and
retry.
PTB-Warning: Read "help InitializePsychSound" for more info about ASIO
et al.
PTB-INFO: New audio device with handle 0 opened as PortAudio stream:
PTB-INFO: For 1 channels Playback: Audio subsystem is MME, Audio
device name is Microsoft Sound Mapper - Output
PTB-INFO: Real samplerate 44100.000000 Hz. Input latency 0.000000
msecs, Output latency 464.399093 msecs.
Then I decided to try another aproch. Lets play the song on other 2 earplugs
I used PsychPortAudio('GetDevices') to find the id of the earplugs pair. Strange is that instead of 3 devices with 2 channels I found 4.
And I used PsychPortAudio('Open' for id 7,9,18 and 20 but every time the song was played on the same earplug pair, the same pair from when I tried to play on just one earplug.
This is a picture with the 4 devices
function BasicSoundOutputDemo( wavfilename)
AssertOpenGL;
% Read WAV file from filesystem:
[y, freq] = psychwavread(wavfilename);
wavedata = y' ;
nrchannels = size(wavedata,1); % Number of rows == number of channels.
InitializePsychSound;
devices = PsychPortAudio('GetDevices' );
pahandle = PsychPortAudio('Open', 18, [], 0, freq, nrchannels);
PsychPortAudio('FillBuffer', pahandle, wavedata);
t1 = PsychPortAudio('Start', pahandle, 1, 0);
KbReleaseWait;
while ~KbCheck
% Wait a seconds...
WaitSecs(1);
end
PsychPortAudio('Stop', pahandle);
PsychPortAudio('Close', pahandle);
fprintf('Demo finished, bye!\n');
Now the only thing that is different at warnigs it this
PTB-INFO: For 2 channels Playback: Audio subsystem is Windows
DirectSound, Audio device name is Speakers (USB Multi-Channel Audio
Device)
Sorry for this long post but i wanted to give you all the informations.
Can you tell were I am wrong. How can I play one song on a specific earplug. I think if I know that then I just copy the code and put another song and so I will play one song on each earplug
1) You likely don't want to run AssertOpenGL every time you present a sound.
2) Your code looks correct, though interestingly on my Apple laptop and built-in sound, sending a single channel signal is also playing from both headphone channels.
3) What audio device are you using? From your device listing, it looks like the 4 you have listed might be different interfaces to the same devices (2 outputs (one digital, one analog) X two APIs (one MME, one DirectSound). Are there any other entries in your device list?
a partial answer to my question. I found how to play a song in an earplug and another in the other. psychwavread gives me a 2 rows array. so i put in an array the first row of one song and the first row of the other song. so now i have a 2 row array simillar to the one from when i play one song, but now i play 2 songs
[y, freq] = psychwavread(wavfilename1);
[y1, freq1] = psychwavread(wavfilename2);
aux = y';
aux1 = y1';
wavedata = [aux1(1,:) ; aux(1,:)];
so i manage to play on 4 channels i am sure that i can play on 6 and 8. I installed ASIO4ALL and I choose the ASIO4ALL device id when open. when ASIO4all opens I can choose my 6 channels device and after that I just choose in the open function on which channels to play the sounds
I tried to use analog input signal for triggering purpose, however I have a problem with analog input reading. For example, when I send 6 V, I can read only 0.5 V, and signal form shown in the link is supposed to be square pulse but obviously, it is not.
My Daq card is NI PCI 6120. I used MAX software to check if it is a hardware issue, but it gives correct value and signal form, and as I try a session based matlab code to read only one analog input channel, I can get correct signal.
There should be a mistake in my matlab triggering code. Any suggestion ?
dig= digitalio('nidaq','Dev1');
line = addline(dio,0:1,'Out');
ai = analoginput('nidaq','Dev1');
channel = addchannel(ai,0:1);
set(ai,'SampleRate',fs);
set(ai,'SamplesPerTrigger',N);
set(ai,'Timeout',10000)
set(ai,'TriggerChannel',channel(1));
set(ai,'TriggerType','Software');
set(ai,'TriggerCondition','Rising');
set(ai,'TriggerConditionValue',0.5);
set(ai,'TriggerDelayUnits','Samples');
set(ai,'TriggerDelay',-3000);
set(ai,'LogFileName','file00.daq')
set(ai,'LoggingMode','Disk&Memory')
putvalue(dig,1)
start (ai)
[data t] = getdata(ai);
putvalue(dio,0)
delete(ai);
delete(dig);
enter image description here
I am trying to send data from MATLAB to ARDUINO using the following code for arduino and the second one for the matlab. Both code work fine and when i press 1 led lights up and when press 2 led become off. But actually what i am trying to do is when matlab run code it automatically send 1 to arduino and the led become on. i have tried may changes but cant able to do. when iam trying to run the third code (given below) the arduino status led blink that show it receive some thing but my actual led which is connected to pin 13 still off.
int ledPin=13;
int matlabData;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin,LOW); // turn light off
}
}
(MATLAB)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.end
13.
14.fclose(arduino); % end communication with arduino
(MY edit code)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.%while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer='1'%('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.%end
13.
14.fclose(arduino); % end communication with arduino
The difference is the following:
answer = input('bla')
yields an answer that is a number, i.e. answer is of type double. In your third case, you wrote answer='1' which is a char, so in fact, the variable answer is different in both cases. Try and change the code in the third part to this here:
answer = 1;
or change the fprintf command to
fprintf(arduino, '%s', str2num(answer));
I am sending 1 from MATLAB to Arduino. My Arduino code works perfectly when I send 1 from teraterm terminal software. But when I send 1 from MATLAB by using this code arduino TX lite blinks but cannot get the 1 output.
s = serial('COM7','BaudRate',9600);
fopen(s)
fprintf(s,'1');
fclose(s)
Problem with this code is that you are not giving Delay to process the code.
According to my calculation : fopen(string) command needs 0.8754 Seconds of Processing time.
So add 1 second delay, it will work.
..
Your Modified Code :
clear all
clc
s=serial('COM7','BaudRate',9600);
fopen(s);
pause(1);
fprintf(s,1);
fclose(s);
I would like to control a piezo driven z-stage (for microscopy) through a simple analog output (0-10V) by using a NI DAQ board and the Matlab session-based interface. This works so far but I need to dynamically update the z-postion very rapidly (best under 20 ms). However queuing and executing an updated voltage value takes over 150 ms right now. Is there any way to optimize this? Thanks!
Here is my code:
%% initialize DAQ, specify output channels and refreshrate
devices = daq.getDevices
s = daq.createSession('ni');
s.addAnalogOutputChannel('Dev1', 'ao0', 'Voltage');
s.Rate = 1000; %1ms refreshrate
%define output data (I only define one data point as DAQ board keeps that voltage until changed)
zVol = 4.8; % range of stage is 100 um => 10um/V
%Queue output data
s.queueOutputData(zVol);
%output signal
s.startBackground();