/src/main/jni/frontend/main.c

https://github.com/n8han/shouty · C · 117 lines · 69 code · 20 blank · 28 comment · 3 complexity · 7b82956c7dee6cc09f9ef2d9211572db MD5 · raw file

  1. /*
  2. * Command line frontend program
  3. *
  4. * Copyright (c) 1999 Mark Taylor
  5. * 2000 Takehiro TOMINAGA
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <jni.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define MAX_U_32_NUM 0xFFFFFFFF
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif
  30. #include <assert.h>
  31. #include <stdio.h>
  32. # ifndef HAVE_STRCHR
  33. # define strchr index
  34. # define strrchr rindex
  35. # endif
  36. char *strchr(), *strrchr();
  37. # ifndef HAVE_MEMCPY
  38. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  39. # define memmove(d, s, n) bcopy ((s), (d), (n))
  40. # endif
  41. #ifdef HAVE_FCNTL_H
  42. # include <fcntl.h>
  43. #endif
  44. #ifdef __sun__
  45. /* woraround for SunOS 4.x, it has SEEK_* defined here */
  46. #include <unistd.h>
  47. #endif
  48. #if defined(_WIN32)
  49. # include <windows.h>
  50. #endif
  51. /*
  52. main.c is example code for how to use libmp3lame.a. To use this library,
  53. you only need the library and lame.h. All other .h files are private
  54. to the library.
  55. */
  56. #include "brhist.h"
  57. #include "parse.h"
  58. #include "main.h"
  59. #include "get_audio.h"
  60. #include "portableio.h"
  61. #include "timestatus.h"
  62. /* PLL 14/04/2000 */
  63. #if macintosh
  64. #include <console.h>
  65. #endif
  66. #ifdef WITH_DMALLOC
  67. #include <dmalloc.h>
  68. #endif
  69. #include <android/log.h>
  70. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "libnav", __VA_ARGS__)
  71. JNIEXPORT jint JNICALL
  72. Java_spur_shouty_NativeEncode_pcm2mp3(JNIEnv* env,
  73. jobject thiz,
  74. jshortArray _pcm,
  75. jint pcmlen,
  76. jint sampleRate,
  77. jbyteArray _mp3) {
  78. static lame_global_flags *gf;
  79. if (!gf) {
  80. LOGV("initializing LAME");
  81. gf = lame_init();
  82. lame_set_num_channels(gf, 1);
  83. lame_set_in_samplerate(gf, sampleRate);
  84. lame_init_params(gf);
  85. }
  86. jshort *pcm = (*env)->GetShortArrayElements(env, _pcm, NULL);
  87. jbyte *mp3 = (*env)->GetByteArrayElements(env, _mp3, NULL);
  88. jsize mp3len = (*env)->GetArrayLength(env, _mp3);
  89. int ret;
  90. if (pcmlen == 0)
  91. ret = lame_encode_flush(gf, mp3, mp3len);
  92. else
  93. ret = lame_encode_buffer(gf, pcm, NULL, pcmlen, mp3, mp3len);
  94. (*env)->ReleaseShortArrayElements(env, _pcm, pcm, 0);
  95. (*env)->ReleaseByteArrayElements(env, _mp3, mp3, 0);
  96. return ret;
  97. }