Difference between revisions of "Link"

From Mumble Wiki
Jump to: navigation, search
(Initialization)
(Initialization)
Line 36: Line 36:
  
 
LinkedMem *lm = NULL;
 
LinkedMem *lm = NULL;
 +
 +
 +
void initMumble() {
  
 
#ifdef WIN32
 
#ifdef WIN32
static HANDLE hMapObject = NULL;
+
HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");
void initMumble() {
 
hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");
 
 
if (hMapObject == NULL)
 
if (hMapObject == NULL)
 
return;
 
return;
Line 50: Line 51:
 
return;
 
return;
 
}
 
}
wcscpy_s(lm->name, 256, L"MyGameName");
 
}
 
 
#else
 
#else
static int shmfd = -1;
+
char memname[256];
void initMumble() {
+
snprintf(memname, 256, "/MumbleLink.%d", getuid());
  char memname[256];
 
  snprintf(memname, 256, "/MumbleLink.%d", getuid());
 
  
  shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);
+
int shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);
  
  if(shmfd < 0) {
+
if (shmfd < 0) {
    return;
+
return;
  }
+
}
  
  lm = (LinkedMem *) (mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));
+
lm = (LinkedMem *)(mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));
  
  if (lm == (void *) (-1)) {
+
if (lm == (void *)(-1)) {
lm = NULL;
+
lm = NULL;
  }
+
return;
}
+
}
 
#endif
 
#endif
 +
memset(lm, 0, sizeof(LinkedMem));
  
 +
wcsncpy(lm->name, L"TestLink", 256);
 +
wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048);
 +
}
 
</pre>
 
</pre>
  
Line 77: Line 78:
  
 
<pre>
 
<pre>
 
 
#ifndef WIN32
 
#ifndef WIN32
 
static const int32_t GetTickCount() {
 
static const int32_t GetTickCount() {
Line 90: Line 90:
 
if (! lm)
 
if (! lm)
 
return;
 
return;
 +
lm->uiVersion = 2;
 +
lm->uiTick = GetTickCount();
 +
 +
// Left handed coordinate system.
 +
// X positive towards "right".
 +
// Y positive towards "up".
 +
// Z positive towards "into screen".
 +
//
 +
// 1 unit = 1 meter
  
// Fill lm->fAvatarPosition, fAvatarTop and fAvatarFront
+
// Unit vector pointing out of the avatars eyes (here Front looks into scene).
        // lm->fCameraPosition, fCameraTop and fCameraFront
+
lm->fAvatarFront[0] = 0.0f;
 +
lm->fAvatarFront[1] = 0.0f;
 +
lm->fAvatarFront[2] = 1.0f;
  
lm->uiVersion = 2;
+
// Unit vector pointing out of the avatars head (here Top looks straight up).
lm->uiTick = GetTickCount();
+
lm->fAvatarTop[0] = 0.0f;
 +
lm->fAvatarTop[1] = 1.0f;
 +
lm->fAvatarTop[2] = 0.0f;
 +
 
 +
// Position of the avatar (here Standing slightly off the origin)
 +
lm->fAvatarPosition[0] += 0.001f;
 +
lm->fAvatarPosition[1] = 0.0f;
 +
lm->fAvatarPosition[2] = 0.5f;
 +
 
 +
// Same as avatar but for the camera. This only has to be set if your games camera
 +
// is independant of your avatars state (e.g. 3rd person view).
 +
lm->fCameraPosition[0] = 0.0f;
 +
lm->fCameraPosition[1] = 0.0f;
 +
lm->fCameraPosition[2] = 0.0f;
 +
 
 +
lm->fCameraFront[0] = 0.0f;
 +
lm->fCameraFront[1] = 0.0f;
 +
lm->fCameraFront[2] = 1.0f;
 +
 
 +
lm->fCameraTop[0] = 0.0f;
 +
lm->fCameraTop[1] = 1.0f;
 +
lm->fCameraTop[2] = 0.0f;
 +
 
 +
// Identifier which uniquely identifies a certain player in a context (e.g. the ingame Name).
 +
wcsncpy(lm->identity, L"Unique ID", 256);
 +
// Context should be equal for players which should be able to hear each other positional and
 +
// differ for those who shouldn't (e.g. it could contain the server+port and team)
 +
memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16);
 +
lm->context_len = 16;
 
}
 
}
 
</pre>
 
</pre>

Revision as of 17:33, 30 November 2009

Linking a game to Mumble

If you can change the sourcecode of the game you want to add positional audio support to you can save yourself the trouble of creating your own plugin by using the facilities supplied by the link plugin.

Note: Please don't forget to add your game to the list of link plugin supported games.

Initialization

Somewhere in your game initalization, add these lines of code:

struct LinkedMem {
#ifdef WIN32
	UINT32	uiVersion;
	DWORD	uiTick;
#else
	uint32_t uiVersion;
	uint32_t uiTick;
#endif
	float	fAvatarPosition[3];
	float	fAvatarFront[3];
	float	fAvatarTop[3];
	wchar_t	name[256];
	float	fCameraPosition[3];
	float	fCameraFront[3];
	float	fCameraTop[3];
	wchar_t	identity[256];
#ifdef WIN32
	UINT32	context_len;
#else
	uint32_t context_len;
#endif
	unsigned char context[256];
	wchar_t description[2048];
};

LinkedMem *lm = NULL;


void initMumble() {

#ifdef WIN32
	HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");
	if (hMapObject == NULL)
		return;

	lm = (LinkedMem *) MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));
	if (lm == NULL) {
		CloseHandle(hMapObject);
		hMapObject = NULL;
		return;
	}
#else
	char memname[256];
	snprintf(memname, 256, "/MumbleLink.%d", getuid());

	int shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);

	if (shmfd < 0) {
		return;
	}

	lm = (LinkedMem *)(mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));

	if (lm == (void *)(-1)) {
		lm = NULL;
		return;
	}
#endif
	memset(lm, 0, sizeof(LinkedMem));

	wcsncpy(lm->name, L"TestLink", 256);
	wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048);
}

Then, for each frame, do the following:

#ifndef WIN32
static const int32_t GetTickCount() {
	struct timeval tv;
	gettimeofday(&tv,NULL);

	return tv.tv_usec / 1000 + tv.tv_sec * 1000;
}
#endif

void updateMumble() {
	if (! lm)
		return;
	lm->uiVersion = 2;
	lm->uiTick = GetTickCount();

	// Left handed coordinate system.
	// X positive towards "right".
	// Y positive towards "up".
	// Z positive towards "into screen".
	//
	// 1 unit = 1 meter

	// Unit vector pointing out of the avatars eyes (here Front looks into scene).
	lm->fAvatarFront[0] = 0.0f;
	lm->fAvatarFront[1] = 0.0f;
	lm->fAvatarFront[2] = 1.0f;

	// Unit vector pointing out of the avatars head (here Top looks straight up).
	lm->fAvatarTop[0] = 0.0f;
	lm->fAvatarTop[1] = 1.0f;
	lm->fAvatarTop[2] = 0.0f;

	// Position of the avatar (here Standing slightly off the origin)
	lm->fAvatarPosition[0] += 0.001f;
	lm->fAvatarPosition[1] = 0.0f;
	lm->fAvatarPosition[2] = 0.5f;

	// Same as avatar but for the camera. This only has to be set if your games camera
	// is independant of your avatars state (e.g. 3rd person view).
	lm->fCameraPosition[0] = 0.0f;
	lm->fCameraPosition[1] = 0.0f;
	lm->fCameraPosition[2] = 0.0f;

	lm->fCameraFront[0] = 0.0f;
	lm->fCameraFront[1] = 0.0f;
	lm->fCameraFront[2] = 1.0f;

	lm->fCameraTop[0] = 0.0f;
	lm->fCameraTop[1] = 1.0f;
	lm->fCameraTop[2] = 0.0f;

	// Identifier which uniquely identifies a certain player in a context (e.g. the ingame Name).
	wcsncpy(lm->identity, L"Unique ID", 256);
	// Context should be equal for players which should be able to hear each other positional and
	// differ for those who shouldn't (e.g. it could contain the server+port and team)
	memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16);
	lm->context_len = 16;
}

fAvatarPosition should be the player position in 3D space. fAvatarTop and fAvatarFront should be the orientation. The coordinate system is a left-handed one, and the units are in meters.

If the camera of your game is independent of the avatar be sure to also fill the fCameraPosition|Top|Front variables.

The identity does not need to be updated every single frame. Identity should contain a string which uniquely identifies the player on the current server (e.g. player name).

The context string is used to determine which users on a Mumble server are in the same game and hence should hear audio positional. If players are on the same server in the same game their contexts should be equal.

Mumble fetches these values 50 times a second, so please update them every frame.