Skip to content

Commit 63f1210

Browse files
committed
return value from AUDupdate
1 parent 518ea4b commit 63f1210

File tree

7 files changed

+152
-49
lines changed

7 files changed

+152
-49
lines changed

cli/cliMsg.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,24 @@ OBJ BgnMsgsend(const char *hostname, int channel)
9090
o->addr.sin_family = AF_INET;
9191
o->addr.sin_addr.s_addr = inet_addr(hostname);
9292
o->addr.sin_port = htons(channel);
93-
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
94-
{
95-
memset((char *)&cl_addr, 0, sizeof(cl_addr));
96-
cl_addr.sin_family = AF_INET;
97-
cl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
98-
cl_addr.sin_port = htons(0);
99-
if (bind(sockfd, (struct sockaddr *)&cl_addr,
100-
sizeof(cl_addr)) < 0)
101-
{
102-
perror("can't bind");
103-
close(sockfd);
104-
sockfd = -1;
105-
}
106-
}
107-
else
108-
printf("unable to make socket\n");
93+
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
94+
printf("Failed to make socket.\n");
95+
return (OBJ)0;
96+
}
97+
memset((char *)&cl_addr, 0, sizeof(cl_addr));
98+
cl_addr.sin_family = AF_INET;
99+
cl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
100+
cl_addr.sin_port = htons(0);
101+
if (bind(sockfd, (struct sockaddr *)&cl_addr, sizeof(cl_addr)) < 0) {
102+
close(sockfd);
103+
perror("failed to bind socket");
104+
return (OBJ)0;
105+
}
109106

110107
fcntl(sockfd, F_SETFL, FNDELAY); // Non-blocking I/O
111108
o->sockfd = sockfd;
112109
o->len = sizeof(o->addr);
113-
#ifdef NOISY
114-
printf("will send to %s:%d\n", inet_ntoa(o->addr.sin_addr), o->channel);
115-
#endif
110+
// printf("will send to %s:%d\n", inet_ntoa(o->addr.sin_addr), o->channel);
116111
return o;
117112
}
118113

@@ -195,7 +190,7 @@ void MsgsendObj(OBJ obj, struct sockaddr_in* paddr, const char* msg)
195190
paddr = &o->addr;
196191
VSS_StripZerosInPlace(msg);
197192
const size_t cb = strlen(msg) + 1; // +1 is the terminating null.
198-
#ifdef VERBOSE
193+
#ifdef NOISY
199194
printf("\t\033[32msend \"%s\"\033[0m\n", msg);
200195
#endif
201196
if (!sendudp(paddr, o->sockfd, cb, msg))

cli/parseMsg.y

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,13 @@ float AUDupdateFloats(int fdT, char* szActor, int numFloats, ...)
533533

534534
extern "C" void VSS_StripZerosInPlace(char*); // in cliMsg.c
535535

536+
// Call AUDupdateWaitForReply(true) before AUDupdateXXX'ing a MG to which you'd added
537+
// BeginSound's or Create's, whose returned handles you want.
538+
static int vfAUDupdateWaitForReply = false;
539+
extern "C" void AUDupdateWaitForReply(int fWait) {
540+
vfAUDupdateWaitForReply = fWait;
541+
}
542+
536543
// Send a data array to the message group called szActor at server fdT.
537544
// Complain if there is no such message group at fdT.
538545
extern "C" float AUDupdate(int fdT, char* szActor, int numFloats, float* floatArray)
@@ -563,7 +570,10 @@ extern "C" float AUDupdate(int fdT, char* szActor, int numFloats, float* floatAr
563570
}
564571
strcat(szT, "]");
565572
VSS_StripZerosInPlace(szT);
566-
return actorMessageRetval(szT);
573+
if (vfAUDupdateWaitForReply) // Set only by AUDupdateWaitForReply.
574+
return actorMessageRetval(szT);
575+
actorMessage(szT);
576+
return hNil; // ignored
567577
}
568578

569579
// Call AUDupdate for two servers, e.g. for wet and dry localization.

cli/vssClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ float AUDupdateFloats(int fdT, char* szActor, int numFloats, ...);
2828
float AUDupdate(int fileHandle, char *messageGroupName, int numFloats, float *floatArray);
2929
void AUDupdateTwo(int theFirst, int theSecond, char *messageGroupName, int numFloats, float *floatArray);
3030
void AUDupdateMany(int numHandles, int * handleArray, char *messageGroupName, int numFloats, float *floatArray);
31+
void AUDupdateWaitForReply(int fWait);
3132
void AUDqueue(int, float*, float);
3233
void AUDflushQueue(int, char *, int fPreserveQueueData );
3334
void actorMessage(const char* messagename);

util/dynamic.aud

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ later = Create LaterActor;
77

88
// Create a sound.
99
NewX = Create MessageGroup;
10-
AddMessage NewX BeginSound fm SetAmp 0.05 SetCarFreq *0;
10+
AddMessage NewX BeginSound fm SetAmp 0.12 SetCarFreq *0;
1111

12-
// Fade out and then delete a sound.
12+
// Modify it.
13+
ChangeFreqX = Create MessageGroup;
14+
AddMessage ChangeFreqX SetCarFreq *0 *1;
15+
16+
// Fade out and then delete it.
1317
DeleteX = Create MessageGroup;
1418
AddMessage DeleteX SetAmp *0 0 .15;
1519
AddMessage DeleteX AddMessage later .16 Delete *0;
1620

17-
// Modify a sound.
18-
ChangeFreqX = Create MessageGroup;
19-
AddMessage ChangeFreqX SetCarFreq *0 *1;
20-
21-
// Save the output to an audio file.
21+
// Save to /tmp/audio-out.raw.aiff.
2222
EnableOfile 1 "/tmp/audio-out.raw";
2323
Done = Create MessageGroup;
2424
AddMessage Done EnableOfile 0;

util/dynamic.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// An example of using AUDupdate()'s return value.
1+
// An example using AUDupdate's return value, via AUDupdateWaitForReply.
22

33
#include "vssClient.h"
44

@@ -9,34 +9,33 @@ int main(int argc, char* argv[])
99
if (!BeginSoundServer())
1010
return -1;
1111

12-
// Ignore AUDinit()'s return value, and thus also don't bother to AUDterminate().
12+
// Ignore the return value, and thus also don't bother to AUDterminate().
1313
(void)AUDinit("dynamic.aud");
1414

15-
// Create sounds.
16-
for (i=0; i<10; i++)
17-
{
15+
// Create 10 sounds.
16+
AUDupdateWaitForReply(1);
17+
for (i=0; i<10; i++) {
1818
sounds[i] = AUDupdateSimpleFloats("NewX", 1, (float)(300 + 100 * i));
19-
usleep(30000);
20-
}
19+
usleep(40000);
20+
}
21+
AUDupdateWaitForReply(0);
2122
usleep(250000);
2223

23-
// Modify sounds.
24-
for (i=0; i<10; i++)
25-
{
24+
// Modify them.
25+
for (i=0; i<10; i++) {
2626
AUDupdateSimpleFloats("ChangeFreqX", 2, sounds[i], (float)(80+40*i));
27-
usleep(50000);
28-
}
27+
usleep(60000);
28+
}
2929
usleep(500000);
3030

31-
// Delete sounds.
32-
for (i=0; i<10; i++)
33-
{
31+
// Delete them.
32+
for (i=0; i<10; i++) {
3433
AUDupdateSimpleFloats("DeleteX", 1, sounds[i]);
35-
sounds[i] = hNil; // Prevent "dangling" handles.
36-
}
37-
usleep(300000);
34+
}
35+
usleep(200000);
3836

3937
AUDupdateSimple("Done", 0, NULL);
38+
usleep(200000);
4039
EndSoundServer();
4140
return 0;
4241
}

util/dynamic.rb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'socket'
4+
$sock = UDPSocket.new
5+
$sock.connect 'localhost', 7999
6+
7+
def send(s) $sock.send(s, 0) end
8+
9+
def get() $sock.recvfrom(500)[0] end # e.g., "AckNoteMsg 7.00"
10+
11+
def gethandle(s) send(s); get.split[1].to_i end
12+
13+
def done() $sock.close end
14+
15+
send 'SetPrintCommands 2'
16+
send 'EnableOfile 1 /tmp/audio-out.raw'
17+
fm = gethandle 'Create FmActor'
18+
19+
UseMessageGroups = false
20+
if UseMessageGroups
21+
22+
# AUDupdateSimpleFloats()
23+
def update(hMG, floats, fGetReply=false)
24+
msg = "SendData #{hMG} [ " + floats.join(' ') + "]";
25+
fGetReply ? gethandle(msg) : send(msg)
26+
end
27+
28+
newX = gethandle 'Create MessageGroup'
29+
send "AddMessage #{newX} BeginSound #{fm} SetAmp 0.12 SetCarFreq *0"
30+
31+
changeFreqX = gethandle 'Create MessageGroup'
32+
send "AddMessage #{changeFreqX} SetCarFreq *0 *1"
33+
34+
deleteX = gethandle 'Create MessageGroup'
35+
later = gethandle 'Create LaterActor'
36+
send "AddMessage #{deleteX} SetAmp *0 0 .15"
37+
send "AddMessage #{deleteX} AddMessage #{later} .16 Delete *0"
38+
39+
# Create 10 sounds.
40+
sounds = (0..10).map {|i|
41+
sleep 0.04
42+
update newX, [300 + 100 * i], true
43+
}
44+
sleep 0.25
45+
46+
# Modify them.
47+
sounds.each_with_index {|h,i|
48+
sleep 0.06
49+
update changeFreqX, [h, 80+40*i]
50+
}
51+
sleep 0.5
52+
53+
# Fade out and delete them.
54+
sounds.each {|h|
55+
update deleteX, [h]
56+
}
57+
sleep 0.2
58+
59+
else
60+
61+
# Create 10 sounds.
62+
sounds = (0..10).map {|i|
63+
sleep 0.04
64+
gethandle "BeginSound #{fm} SetAmp 0.12 SetCarFreq #{300 + 100 * i}"
65+
}
66+
sleep 0.25
67+
68+
# Modify them.
69+
sounds.each_with_index {|h,i|
70+
sleep 0.06
71+
send "SetCarFreq #{h} #{80+40*i}"
72+
}
73+
sleep 0.5
74+
75+
# Fade out and delete them.
76+
sounds.each {|h| send "SetAmp #{h} 0 .15" }
77+
sleep 0.16
78+
sounds.each {|h| send "Delete #{h}" }
79+
sleep 0.2
80+
81+
end
82+
83+
# Save the output to /tmp/audio-out.raw.aiff.
84+
send 'EnableOfile 0'
85+
sleep 0.05
86+
done

vssSrv.c++

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,21 +443,21 @@ int actorMessageHandler(const char* Message)
443443
if (!fKeepRunning)
444444
return 0;
445445

446-
if (printCommands >= 2 && !vfAlreadyLogged)
446+
const auto fInternal = !vfAlreadyLogged; // Not already handled by LiveTick > actorMessageMM > actorMessageHandler.
447+
if (fInternal && printCommands >= 2)
447448
cerr << Message << " (internal)\n";
448449
vfAlreadyLogged = false; // This was the 2nd time. Permit later msgs to be printed.
449450

450451
switch (caught)
451452
{
452453
case 0:
453454
cerr << "vss: ignored message with garbled args: " << Message << "\n";
454-
return 1;
455455
case 1:
456456
return 1;
457457
case 2:
458458
break;
459459
default:
460-
cerr << "vss: actorMessageHandler internal error.\n";
460+
cerr << "vss: actorMessageHandlerCore internal error.\n";
461461
return 1;
462462
}
463463

@@ -478,10 +478,22 @@ int actorMessageHandler(const char* Message)
478478
cerr << "vss: ignored message with unknown actor handle " << h << ": " << Message << "\n";
479479
return 1;
480480
}
481+
// Infer whether or not to ReturnFloatMsgToClient, copied from actorMessageMM.
482+
const auto fReturnToClient = fInternal &&
483+
(!strncmp(Message, "Create ", 7) || !strncmp(Message, "BeginSound", 10 /* or BeginSoundPaused */));
484+
if (fInternal) cerr << "\nInternal msg will reply.\n";
481485
if (!a->receiveMessage(Message)) {
482486
// VActor::receiveMessage complained already.
483487
return 1;
484488
}
489+
if (fReturnToClient) {
490+
// hNil may mean an AUDupdate() "SendData mgFoo [...]" that wants a "*?".
491+
if (vzReturnToClient == hNil)
492+
vzReturnToClient = vzMessageGroupRecentHandle;
493+
ReturnFloatMsgToClient(vzReturnToClient, "AckNoteMsg");
494+
if (printCommands >= 1)
495+
cerr << " = " << vzReturnToClient << "\n";
496+
}
485497
if (a->delete_me() /* set by VActor::receiveMessage */)
486498
delete a;
487499
return 1;

0 commit comments

Comments
 (0)