Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Biophysics Labs
Firmware
Commits
d40c7145
Commit
d40c7145
authored
Jun 17, 2020
by
Your Name
Browse files
more zmqaudio.cc
parent
cb247ef4
Changes
2
Hide whitespace changes
Inline
Side-by-side
lsldert/zmqaudio
100644 → 100755
View file @
d40c7145
No preview for this file type
lsldert/zmqaudio.cc
View file @
d40c7145
//
// zmqaudio.cc -- play audio samples received over zeromq sockets
// Copyright (c) 2020 Günter Winau
// based on the example in paex_sine.c
// based on the example in paex_sine.c
//
//
...
...
@@ -42,9 +42,9 @@
#include
"zmqaudio.h"
#include
"portaudio.h"
#include
"zhelpers.hpp"
#include
<iostream>
#include
<math.h>
#include
<stdio.h>
#include
<iostream>
#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
...
...
@@ -61,23 +61,22 @@ typedef struct {
char
message
[
20
];
}
paTestData
;
int
verbose
=
0
;
const
int
nbuf
=
5
;
zmq_audiobuffer
*
buffer
[
nbuf
];
int
verbose
=
1
;
const
int
n
r
buf
fers
=
5
;
zmq_audiobuffer
*
buffer
[
n
r
buf
fers
];
zmq_audiobuffer
::
zmq_audiobuffer
(
std
::
uint32_t
_fsamp
,
std
::
uint32_t
_nsamp
,
std
::
uint32_t
_nchan
)
:
fsamp
{
_fsamp
},
nsamp
{
_nsamp
},
nchan
{
_nchan
},
data
{
new
float
[
_nsamp
*
_nchan
]},
idata
{
0
},
frames_per_buffer
{
64
},
playing
{
false
},
stream
{
nullptr
}
{
std
::
uint32_t
_nchan
)
:
fsamp
{
_fsamp
},
nsamp
{
_nsamp
},
nchan
{
_nchan
},
data
{
new
float
[
_nsamp
*
_nchan
]},
idata
{
0
},
frames_per_buffer
{
64
},
playing
{
false
},
stream
{
nullptr
}
{
assert
(
sizeof
(
float
)
==
4
);
// need 32 bit IEEE 754 floats
PaStreamParameters
outputParameters
;
outputParameters
.
device
=
Pa_GetDefaultOutputDevice
();
/* default output device */
if
(
outputParameters
.
device
==
paNoDevice
)
{
fprintf
(
stderr
,
"Error: No default output device.
\n
"
);
return
1
;
return
;
}
outputParameters
.
channelCount
=
nchan
;
/* stereo output */
outputParameters
.
sampleFormat
=
...
...
@@ -93,18 +92,15 @@ zmq_audiobuffer::zmq_audiobuffer(std::uint32_t _fsamp, std::uint32_t _nsamp,
don't bother clipping them */
zmq_audiobuffer
::
stream_callback
,
this
);
if
(
err
!=
paNoError
)
return
err
;
return
;
}
zmq_audiobuffer
::~
zmq_audiobuffer
()
{
zmq_audiobuffer
::~
zmq_audiobuffer
()
{
delete
data
;
Pa_StopStream
(
stream
);
Pa_CloseStream
(
stream
);
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
...
...
@@ -148,7 +144,7 @@ int zmq_audiobuffer::play() {
idata
=
0
;
sprintf
(
message
,
"No Message"
);
err
=
int
err
=
Pa_SetStreamFinishedCallback
(
stream
,
&
zmq_audiobuffer
::
stream_finished
);
if
(
err
!=
paNoError
)
return
err
;
...
...
@@ -158,17 +154,14 @@ int zmq_audiobuffer::play() {
return
err
;
playing
=
true
;
//printf("Play for %d seconds.\n", NUM_SECONDS);
//Pa_Sleep((unsigned int)(duration() * 1000));
// printf("Play for %d seconds.\n", NUM_SECONDS);
// Pa_Sleep((unsigned int)(duration() * 1000));
return
paNoError
;
}
void
zmq_audiobuffer
::
abort
()
{
Pa_AbortStream
(
stream
);
}
void
zmq_audiobuffer
::
abort
()
{
Pa_AbortStream
(
stream
);
}
void
zmq_audiobuffer
::
stop
()
{
Pa_StopStream
(
stream
);
}
void
zmq_audiobuffer
::
stop
()
{
Pa_StopStream
(
stream
);
}
void
zmq_audiobuffer
::
fill
()
{
// initialise sinusoidal wavetable
...
...
@@ -206,14 +199,15 @@ int zmq_recv_multi(zmq::socket_t &socket, zmq::message_t parts[], int nmax) {
zmq
::
message_t
discard
;
socket
.
recv
(
&
discard
);
if
(
verbose
)
std
::
cout
<<
"recv part "
<<
n
<<
" (discarded)."
<<
std
::
endl
;
std
::
cout
<<
"recv part "
<<
n
<<
" (discarded)."
<<
std
::
endl
;
}
socket
.
getsockopt
(
ZMQ_RCVMORE
,
&
more
,
&
more_size
);
}
while
(
more
);
}
catch
(
std
::
exception
&
error
)
{
std
::
cout
<<
"zmq_rev_multi while receiving frame "
<<
n
<<
": "
<<
error
.
what
()
<<
std
::
endl
;
<<
error
.
what
()
<<
std
::
endl
;
}
return
n
;
}
...
...
@@ -229,9 +223,9 @@ int main(void) {
zmq
::
context_t
context
(
1
);
zmq
::
socket_t
ssub
(
context
,
ZMQ_SUB
);
ssub
.
connect
(
"tcp://
lsldert00
:5557"
);
ssub
.
connect
(
"tcp://
raspi6.local
:5557"
);
// ssub.connect("tcp://lsldert00.local:5557");
ssub
.
setsockopt
(
ZMQ_SUBSCRIBE
,
"A"
,
0
);
ssub
.
setsockopt
(
ZMQ_SUBSCRIBE
,
"A"
,
1
);
int
run
=
1
;
while
(
run
)
{
...
...
@@ -243,38 +237,36 @@ int main(void) {
zmq
::
message_t
&
request
=
msg
[
0
];
std
::
string
str
((
char
*
)
request
.
data
());
if
(
verbose
)
{
std
::
cout
<<
"Received: "
<<
str
<<
" ("
<<
request
.
size
()
<<
" bytes)"
<<
std
::
endl
;
if
(
nmsg
>
1
)
{
for
(
int
i
=
1
;
i
<
nmsg
;
i
++
)
std
::
cout
<<
" ...one more message, "
<<
msg
[
i
].
size
()
<<
" bytes"
<<
std
::
endl
;
}
if
(
verbose
)
{
std
::
cout
<<
"Received: '"
<<
str
<<
"' ("
<<
request
.
size
()
<<
" bytes)"
<<
std
::
endl
;
if
(
nmsg
>
1
)
{
for
(
int
i
=
1
;
i
<
nmsg
;
i
++
)
std
::
cout
<<
" ...one more message, "
<<
msg
[
i
].
size
()
<<
" bytes"
<<
std
::
endl
;
}
}
// parse the command and data in the message
std
::
istringstream
s
(
str
);
std
::
string
cmd
;
s
>>
cmd
;
if
(
verbose
)
std
::
cout
<<
"cmd="
<<
str
<<
std
::
endl
;
if
(
cmd
==
"AF"
)
{
// Fill
}
else
if
(
cmd
==
"AP"
)
{
// Play
// parse the command and data in the message
std
::
istringstream
s
(
str
);
std
::
string
cmd
;
unsigned
int
ibuf
;
s
>>
cmd
>>
ibuf
;
}
else
if
(
cmd
==
"AS"
)
{
// Stop
if
(
verbose
)
std
::
cout
<<
"cmd='"
<<
cmd
<<
"' ibuf="
<<
ibuf
<<
std
::
endl
;
if
(
ibuf
>=
nrbuffers
)
{
std
::
cerr
<<
"buffer index out of range"
<<
std
::
endl
;
continue
;
}
}
if
(
cmd
==
"AF"
)
{
// Fill
}
else
if
(
cmd
==
"AP"
)
{
// Play
}
else
if
(
cmd
==
"AS"
)
{
// Stop
}
buffer
[
0
]
=
new
zmq_audiobuffer
(
44100
,
5
*
48000
,
2
);
zmq_audiobuffer
*
data
=
buffer
[
0
];
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment