summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/generator/Generator.cpp
blob: f00d1f1c6aa80c3c3f53a1a12b4eda5a6190f615 (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
150
151
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "ace/OS_NS_unistd.h"
#include "Utils.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include "ConfigManager.h"
#include "GeneratorConfig.h"

extern AudioChunkCallBackFunction g_audioChunkCallBack;
extern CaptureEventCallBackFunction g_captureEventCallBack;
extern OrkLogManager* g_logManager;

GeneratorConfigTopObjectRef g_generatorConfigTopObjectRef;
#define GCONFIG g_generatorConfigTopObjectRef.get()->m_config

void Configure(DOMNode* node)
{
	if (node)
	{
		GeneratorConfigTopObjectRef generatorConfigTopObjectRef(new GeneratorConfigTopObject);
		try
		{
			generatorConfigTopObjectRef.get()->DeSerializeDom(node);
			g_generatorConfigTopObjectRef = generatorConfigTopObjectRef;
		}
		catch (CStdString& e)
		{
			LOG4CXX_WARN(g_logManager->rootLog, "Generator.dll: " + e + " - using defaults");
		}
	}
	else
	{
		LOG4CXX_WARN(g_logManager->rootLog, "Generator.dll: got empty DOM tree");
	}
}

void Initialize()
{
	// create a default config object in case it was not properly initialized by Configure
	if (!g_generatorConfigTopObjectRef.get())
	{
		g_generatorConfigTopObjectRef.reset(new GeneratorConfigTopObject);
	}
}

void Run()
{

#define NUM_SAMPLES_PER_CHUNK 8000

	// Load test file data into memory
	int fileSize = 0;
	int audioBufferSize = 0;
	int numChunks;
	short* audioBuffer = NULL;
	FILE* file = fopen((PCSTR)GCONFIG.m_audioFilename,"rb");
	if (file)
	{
		fseek (file, 0, SEEK_END);
        fileSize = ftell(file);
		fseek (file, 0, SEEK_SET);

		// round up file size to the next NUM_SAMPLES_PER_CHUNK multiple
		numChunks = (fileSize/NUM_SAMPLES_PER_CHUNK) + 1;
		fileSize = numChunks * NUM_SAMPLES_PER_CHUNK;

		audioBuffer = (short *)malloc(sizeof(short)*fileSize);
		audioBufferSize = fileSize/sizeof(short);
		for(int i=0; i<fileSize; i++)
		{
			audioBuffer[i] = 0;
		}
		int numRead = fread(audioBuffer, sizeof(short), fileSize, file);
		fclose(file);
	}
	else
	{
		// can't find test file - have a single zeroed buffer in memory
		LOG4CXX_WARN(g_logManager->rootLog, "Generator.dll: Could not load audio test file:" + GCONFIG.m_audioFilename + " using empty buffer instead");

		numChunks = 1;
		audioBuffer = (short *)malloc(sizeof(short)*NUM_SAMPLES_PER_CHUNK);
		audioBufferSize = NUM_SAMPLES_PER_CHUNK;
		for(int i=0; i<NUM_SAMPLES_PER_CHUNK; i++)
		{
			audioBuffer[i] = 0;
		}
	}

	int elapsed = 0;

	for(;;)
	{

		for(int portId = 0; portId<GCONFIG.m_numConcurrentPorts; portId++)
		{
			CStdString portName;
			portName.Format("port%d", portId);

			if ((elapsed%GCONFIG.m_audioDuration)  == 0)
			{
				// signal call stop and start on all ports
				CaptureEventRef stopEvent(new CaptureEvent);
				stopEvent->m_type = CaptureEvent::EtStop;
				stopEvent->m_timestamp = time(NULL);
				g_captureEventCallBack(stopEvent, portName);

				CaptureEventRef startEvent(new CaptureEvent);
				startEvent->m_type = CaptureEvent::EtStart;
				startEvent->m_timestamp = time(NULL);
				g_captureEventCallBack(startEvent, portName);
			}
			// send audio buffer
			AudioChunkRef chunkRef(new AudioChunk);
			int sampleOffset = (elapsed % numChunks)*NUM_SAMPLES_PER_CHUNK;
			AudioChunkDetails details;
			details.m_encoding = PcmAudio;
			details.m_numBytes = sizeof(short) * NUM_SAMPLES_PER_CHUNK;
			chunkRef->SetBuffer(audioBuffer+sampleOffset, details);
			g_audioChunkCallBack(chunkRef, portName);
		}

		ACE_OS::sleep(1);
		elapsed++;
	}
}


void __CDECL__ StartCapture(CStdString& capturePort)
{
	;
}

void __CDECL__ StopCapture(CStdString& capturePort)
{
	;
}