aboutsummaryrefslogtreecommitdiff
path: root/test_OSC.c
blob: 4593ec6724a61221915b46565a090be055a813a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (c) 1997 Regents of the University of California.
 * All rights reserved.
 *
 * The name of the University may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
 * IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/*
   test_OSC.c
   Trivial program to test OpenSoundControl.[ch]

   Matt Wright 6/2/97
*/

#include <stdio.h>
#include <ctype.h>
#include "OpenSoundControl.h"

#define SIZE 10000

void PrintBuf(OSCbuf *b) {
    printf("Buffer is %sempty.\n", OSC_isBufferEmpty(b) ? "" : "not ");
    printf("%d bytes free in buffer\n", OSC_freeSpaceInBuffer(b));
    printf("Buffer is %sready to send\n", OSC_isBufferDone(b) ?"":"not ");

    printf("Buffer: bufptr %p, state %d, thisMsgSize %p, bundleDepth %d\n"
	   "prevCounts[%d] %p\n", b->bufptr, b->state, b->thisMsgSize,
	   b->bundleDepth, b->bundleDepth, b->prevCounts[b->bundleDepth]);
}

void PrintPacket(OSCbuf *b) {
    char *p = OSC_getPacket(b);
    int size = OSC_packetSize(b);
    unsigned int *intp;
    int i;

    printf("PrintPacket: packet at %p, size %d\n", p, size);
    if (p == 0 || size == 0) return;

    printf("Hex version:");
    for (i = 0, intp = (unsigned int *)p; i < size; i += 4, intp++) {
	if (i % 40 == 0) printf("\n");
	printf("%x ", *intp);
    }

    printf("\n\nString version:");
    for (i = 0; i < size; i++) {
	if (i % 40 == 0) printf("\n");
	if (isprint(p[i])) {
	    printf("%c", p[i]);
	} else {
	    printf("\\%x", p[i] & 0x000000ff);
	}
    }
    printf("\n");
}
    

main() {
    OSCbuf myBuf;
    OSCbuf *b = &myBuf;
    char bytes[SIZE];
    OSCTimeTag tt;

    printf("OSC_initBuffer\n");
    OSC_initBuffer(b, SIZE, bytes);

    PrintBuf(b);

    printf("Testing one-message packet\n");
    if (OSC_writeAddress(b, "/blah/bleh/singlemessage")) {
	printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeFloatArg(b, 1.23456f)) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    {
	float floatarray[10];
	int i;
	for (i = 0; i < 10; ++i) {
	    floatarray[i] = i * 10.0f;
	}
	if (OSC_writeFloatArgs(b, 10, floatarray)) {
	    printf("** ERROR: %s\n", OSC_errorMessage);
	}
    }

    if (OSC_writeIntArg(b, 123456)) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeStringArg(b, "This is a cool string, dude.")) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    PrintBuf(b);
    PrintPacket(b);

    printf("Resetting\n");
    OSC_resetBuffer(b);

    printf("Testing time tags\n");
    tt = OSCTT_CurrentTime();
    printf("Time now is %llx\n", tt);

    printf("Testing bundles\n");
    if (OSC_openBundle(b, tt)) {
	printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeAddress(b, "/a/hello")) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeIntArg(b, 16)) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeIntArg(b, 32)) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_openBundle(b, OSCTT_PlusSeconds(tt, 1.0f))) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeAddress(b, "/b/hello")) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    if (OSC_writeAddress(b, "/c/hello")) {
        printf("** ERROR: %s\n", OSC_errorMessage);
    }

    OSC_closeAllBundles(b);

    PrintBuf(b);
    PrintPacket(b);
}