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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*===============================================================================================
Record example
Copyright (c), Firelight Technologies Pty, Ltd 2004-2011.
This example shows how to record a sound, then write it to a wav file.
It then shows how to play a sound while it is being recorded to. Because it is recording, the
sound playback has to be delayed a little bit so that the playback doesn't play part of the
buffer that is still being written to.
===============================================================================================*/
#include <fmodex/fmod.h>
#include <fmodex/fmod_errors.h>
#include "wincompat.h"
#include <string.h>
void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}
#if defined(WIN32) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
#define __PACKED /* dummy */
#else
#define __PACKED __attribute__((packed)) /* gcc packed */
#endif
/*
[
[DESCRIPTION]
Writes out the contents of a record buffer to a file.
[PARAMETERS]
[RETURN_VALUE]
void
[REMARKS]
]
*/
void SaveToWav(FMOD_SOUND *sound)
{
FILE *fp;
int channels, bits;
float rate;
void *ptr1, *ptr2;
unsigned int lenbytes, len1, len2;
if (!sound)
{
return;
}
FMOD_Sound_GetFormat (sound, 0, 0, &channels, &bits);
FMOD_Sound_GetDefaults(sound, &rate, 0, 0, 0);
FMOD_Sound_GetLength (sound, &lenbytes, FMOD_TIMEUNIT_PCMBYTES);
{
#if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
#pragma pack(1)
#endif
/*
WAV Structures
*/
typedef struct
{
signed char id[4];
int size;
} RiffChunk;
struct
{
RiffChunk chunk __PACKED;
unsigned short wFormatTag __PACKED; /* format type */
unsigned short nChannels __PACKED; /* number of channels (i.e. mono, stereo...) */
unsigned int nSamplesPerSec __PACKED; /* sample rate */
unsigned int nAvgBytesPerSec __PACKED; /* for buffer estimation */
unsigned short nBlockAlign __PACKED; /* block size of data */
unsigned short wBitsPerSample __PACKED; /* number of bits per sample of mono data */
} __PACKED FmtChunk = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits };
struct
{
RiffChunk chunk;
} DataChunk = { {{'d','a','t','a'}, lenbytes } };
struct
{
RiffChunk chunk;
signed char rifftype[4];
} WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} };
#if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
#pragma pack()
#endif
fp = fopen("record.wav", "wb");
/*
Write out the WAV header.
*/
fwrite(&WavHeader, sizeof(WavHeader), 1, fp);
fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp);
fwrite(&DataChunk, sizeof(DataChunk), 1, fp);
/*
Lock the sound to get access to the raw data.
*/
FMOD_Sound_Lock(sound, 0, lenbytes, &ptr1, &ptr2, &len1, &len2);
/*
Write it to disk.
*/
fwrite(ptr1, len1, 1, fp);
/*
Unlock the sound to allow FMOD to use it again.
*/
FMOD_Sound_Unlock(sound, ptr1, ptr2, len1, len2);
fclose(fp);
}
}
int main(int argc, char *argv[])
{
FMOD_SYSTEM *system = 0;
FMOD_SOUND *sound = 0;
//FMOD_CHANNEL *channel = 0;
FMOD_RESULT result;
FMOD_CREATESOUNDEXINFO exinfo;
int key, driver, recorddriver, numdrivers, count;
unsigned int version;
/*
Create a System object and initialize.
*/
result = FMOD_System_Create(&system);
ERRCHECK(result);
result = FMOD_System_GetVersion(system, &version);
ERRCHECK(result);
if (version < FMOD_VERSION)
{
printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
return 0;
}
/*
System initialization
*/
printf("---------------------------------------------------------\n");
printf("Select OUTPUT type\n");
printf("---------------------------------------------------------\n");
printf("1 : OSS - Open Sound System\n");
printf("2 : ALSA - Advanced Linux Sound Architecture\n");
printf("3 : ESD - Enlightenment Sound Daemon\n");
printf("---------------------------------------------------------\n");
printf("Press a corresponding number or ESC to quit\n");
do
{
key = getch();
} while (key != 27 && key < '1' && key > '5');
switch (key)
{
case '1' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_OSS);
break;
case '2' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_ALSA);
break;
case '3' : result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_ESD);
break;
default : return 1;
}
ERRCHECK(result);
/*
Enumerate playback devices
*/
result = FMOD_System_GetNumDrivers(system, &numdrivers);
ERRCHECK(result);
printf("---------------------------------------------------------\n");
printf("Choose a PLAYBACK driver\n");
printf("---------------------------------------------------------\n");
for (count=0; count < numdrivers; count++)
{
char name[256];
result = FMOD_System_GetDriverInfo(system, count, name, 256, 0);
ERRCHECK(result);
printf("%d : %s\n", count + 1, name);
}
printf("---------------------------------------------------------\n");
printf("Press a corresponding number or ESC to quit\n");
do
{
key = getch();
if (key == 27)
{
return 0;
}
driver = key - '1';
} while (driver < 0 || driver >= numdrivers);
result = FMOD_System_SetDriver(system, driver);
ERRCHECK(result);
/*
Enumerate record devices
*/
result = FMOD_System_GetRecordNumDrivers(system, &numdrivers);
ERRCHECK(result);
printf("---------------------------------------------------------\n");
printf("Choose a RECORD driver\n");
printf("---------------------------------------------------------\n");
for (count=0; count < numdrivers; count++)
{
char name[256];
result = FMOD_System_GetRecordDriverInfo(system, count, name, 256, 0);
ERRCHECK(result);
printf("%d : %s\n", count + 1, name);
}
printf("---------------------------------------------------------\n");
printf("Press a corresponding number or ESC to quit\n");
recorddriver = 0;
do
{
key = getch();
if (key == 27)
{
return 0;
}
recorddriver = key - '1';
} while (recorddriver < 0 || recorddriver >= numdrivers);
printf("\n");
result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
ERRCHECK(result);
memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
exinfo.numchannels = 1;
exinfo.format = FMOD_SOUND_FORMAT_PCM16;
exinfo.defaultfrequency = 44100;
exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5;
result = FMOD_System_CreateSound(system, 0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
ERRCHECK(result);
printf("===================================================================\n");
printf("Recording example. Copyright (c) Firelight Technologies 2004-2011.\n");
printf("===================================================================\n");
printf("\n");
printf("Press 'r' to record a 5 second segment of audio and write it to a wav file.\n");
printf("Press 'p' to play the 5 second segment of audio.\n");
printf("Press 'l' to turn looping on/off.\n");
printf("Press 's' to stop recording and playback.\n");
printf("Press 'w' to save the 5 second segment to a wav file.\n");
printf("Press 'Esc' to quit\n");
printf("\n");
/*
Main loop.
*/
do
{
static FMOD_CHANNEL *channel = 0;
static int looping = 0;
int recording = 0;
int playing = 0;
unsigned int recordpos = 0;
unsigned int playpos = 0;
unsigned int length;
if (kbhit())
{
key = getch();
switch (key)
{
case 'r' :
case 'R' :
{
result = FMOD_System_RecordStart(system, recorddriver, sound, looping);
ERRCHECK(result);
break;
}
case 'p' :
case 'P' :
{
if (looping)
{
FMOD_Sound_SetMode(sound, FMOD_LOOP_NORMAL);
}
else
{
FMOD_Sound_SetMode(sound, FMOD_LOOP_OFF);
}
ERRCHECK(result);
result = FMOD_System_PlaySound(system, FMOD_CHANNEL_REUSE, sound, 0, &channel);
ERRCHECK(result);
break;
}
case 'l' :
case 'L' :
{
looping = !looping;
break;
}
case 's' :
case 'S' :
{
result = FMOD_System_RecordStop(system, recorddriver);
if (channel)
{
FMOD_Channel_Stop(channel);
channel = 0;
}
break;
}
case 'w' :
case 'W' :
{
printf("Writing to record.wav ... \r");
SaveToWav(sound);
Sleep(500);
break;
}
}
}
FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
FMOD_System_IsRecording(system, recorddriver, &recording);
ERRCHECK(result);
FMOD_System_GetRecordPosition(system, recorddriver, &recordpos);
ERRCHECK(result);
if (channel)
{
FMOD_Channel_IsPlaying(channel, &playing);
ERRCHECK(result);
FMOD_Channel_GetPosition(channel, &playpos, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
}
printf("State: %-19s. Record pos = %6d : Play pos = %6d : Loop %-3s\r", recording ? playing ? "Recording / playing" : "Recording" : playing ? "Playing" : "Idle", recordpos, playpos, looping ? "On" : "Off");
fflush(stdout);
FMOD_System_Update(system);
Sleep(10);
} while (key != 27);
printf("\n");
/*
Shut down
*/
result = FMOD_Sound_Release(sound);
ERRCHECK(result);
result = FMOD_System_Release(system);
ERRCHECK(result);
return 0;
} |
Partager