AudioCoderFlac.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "AudioCoderFlac.h"
  2. #include <bfc/platform/types.h>
  3. #include <FLAC/metadata.h>
  4. AudioCoderFlac::AudioCoderFlac(unsigned int nch, unsigned int bps, unsigned int samplerate, unsigned int compression)
  5. {
  6. /* initialize stuff first so we can clean up safely if things go wrong */
  7. finished = false;
  8. finishedBytes = 0;
  9. padding = 0;
  10. encoder = 0;
  11. win32State.bytesWritten = 0;
  12. win32State.handle = INVALID_HANDLE_VALUE;
  13. tempFile[0]=0;
  14. wchar_t tempPath[MAX_PATH-14] = {0};
  15. GetTempPath(MAX_PATH-14, tempPath);
  16. GetTempFileName(tempPath, L"wfl", 0, tempFile);
  17. win32State.handle = CreateFile(tempFile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
  18. if (win32State.handle != INVALID_HANDLE_VALUE)
  19. {
  20. this->nch = nch;
  21. this->bps = bps;
  22. encoder = FLAC__stream_encoder_new();
  23. if (!encoder)
  24. return;
  25. // set stream info
  26. if (!FLAC__stream_encoder_set_channels(encoder, nch)
  27. || !FLAC__stream_encoder_set_bits_per_sample(encoder, bps)
  28. || !FLAC__stream_encoder_set_sample_rate(encoder, samplerate)
  29. || !FLAC__stream_encoder_set_total_samples_estimate(encoder, 0)
  30. || !FLAC__stream_encoder_set_compression_level(encoder, compression)
  31. || !FLAC__stream_encoder_set_blocksize(encoder, 0))
  32. {
  33. FLAC__stream_encoder_delete(encoder);
  34. encoder=0;
  35. return;
  36. }
  37. // TODO: set any more config stuff
  38. // TODO: seektable?
  39. //FLAC__StreamMetadata *seektable = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
  40. padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
  41. if (padding)
  42. {
  43. padding->length = 16384; // TODO: configurable padding size
  44. if (!FLAC__stream_encoder_set_metadata(encoder, &padding, 1))
  45. {
  46. FLAC__stream_encoder_delete(encoder);
  47. encoder=0;
  48. return;
  49. }
  50. }
  51. if (FLAC__stream_encoder_init_stream(encoder, Win32_Write, Win32_Seek, Win32_Tell, NULL, &win32State) != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
  52. {
  53. FLAC__stream_encoder_delete(encoder);
  54. encoder=0;
  55. return;
  56. }
  57. }
  58. }
  59. bool AudioCoderFlac::OK()
  60. {
  61. if (!encoder)
  62. return false;
  63. return FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK;
  64. }
  65. AudioCoderFlac::~AudioCoderFlac()
  66. {
  67. if (encoder)
  68. FLAC__stream_encoder_delete(encoder);
  69. if (padding)
  70. FLAC__metadata_object_delete(padding);
  71. if (win32State.handle != INVALID_HANDLE_VALUE)
  72. CloseHandle(win32State.handle);
  73. }
  74. static void Copy8(FLAC__int32 *buffer, void *inputData, int numSamples)
  75. {
  76. uint8_t *in = (uint8_t *)inputData;
  77. for (int i=0;i<numSamples;i++)
  78. {
  79. buffer[i] = (FLAC__int32)in[i];
  80. }
  81. }
  82. static void Copy16(FLAC__int32 *buffer, void *inputData, int numSamples)
  83. {
  84. int16_t *in = (int16_t *)inputData;
  85. for (int i=0;i<numSamples;i++)
  86. {
  87. buffer[i] = (FLAC__int32)in[i];
  88. }
  89. }
  90. static void Copy24(FLAC__int32 *buffer, void *inputData, int numSamples)
  91. {
  92. uint8_t *in = (uint8_t *)inputData;
  93. for (int i=0;i<numSamples;i++)
  94. {
  95. FLAC__int32 val = (((FLAC__int32)in[0]) << 0);
  96. val = val | (((FLAC__int32)in[1]) << 8);
  97. val = val | (((FLAC__int32)in[2]) << 16);
  98. buffer[i] = (FLAC__int32)val;
  99. in+=3;
  100. }
  101. }
  102. static void Copy32(FLAC__int32 *buffer, void *inputData, int numSamples)
  103. {
  104. int32_t *in = (int32_t *)inputData;
  105. for (int i=0;i<numSamples;i++)
  106. {
  107. buffer[i] = (FLAC__int32)in[i];
  108. }
  109. }
  110. int AudioCoderFlac::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
  111. {
  112. FLAC__int32 buffer[65536];
  113. FLAC__uint64 startBytes = win32State.bytesWritten;
  114. if (!in_avail)
  115. {
  116. if (finished)
  117. {
  118. int ret = (int)finishedBytes;
  119. finishedBytes = 0;
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. int numSamples = in_avail/(bps/8);
  125. if (numSamples>65536)
  126. numSamples = 65536;
  127. switch (bps)
  128. {
  129. case 8:
  130. Copy8(buffer, in, numSamples);
  131. break;
  132. case 16:
  133. Copy16(buffer, in, numSamples);
  134. break;
  135. case 24:
  136. Copy24(buffer, in, numSamples);
  137. break;
  138. case 32:
  139. Copy32(buffer, in, numSamples);
  140. break;
  141. }
  142. FLAC__bool result = FLAC__stream_encoder_process_interleaved(encoder, buffer, numSamples/nch);
  143. if (result)
  144. {
  145. *in_used = numSamples*(bps/8);
  146. return (int)(win32State.bytesWritten - startBytes);
  147. }
  148. return 0;
  149. }
  150. void AudioCoderFlac::PrepareToFinish()
  151. {
  152. FLAC__uint64 startBytes = win32State.bytesWritten;
  153. FLAC__stream_encoder_finish(encoder);
  154. finishedBytes = win32State.bytesWritten - startBytes;
  155. }
  156. void AudioCoderFlac::Finish(const wchar_t *destination)
  157. {
  158. if (win32State.handle != INVALID_HANDLE_VALUE)
  159. CloseHandle(win32State.handle);
  160. win32State.handle = INVALID_HANDLE_VALUE;
  161. if (!MoveFile(tempFile, destination))
  162. {
  163. if (CopyFile(tempFile, destination, FALSE))
  164. DeleteFile(tempFile);
  165. }
  166. }