Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

uriahheep
Friday, September 3rd, 2010 at 4:15:59pm MDT 

  1. --- speexclient/speexclient.c   2010-09-03 12:44:29.890652316 -0400
  2. +++ speexclient-vogue/speexclient.c     2010-09-03 18:12:11.470922456 -0400
  3. @@ -46,7 +46,19 @@
  4.  #include <unistd.h> /* close() */
  5.  #include <string.h> /* memset() */
  6.  
  7. -#include "alsa_device.h"
  8. +/* #include "alsa_device.h" */
  9. +
  10. +/* begin playwav2.c paste */
  11. +
  12. +#include <fcntl.h>
  13. +#include <stdint.h>
  14. +#include <sys/mman.h>
  15. +#include <sys/ioctl.h>
  16. +
  17. +#include <linux/ioctl.h>
  18. +
  19. +/* end playwav2.c paste */
  20. +
  21.  #include <speex/speex.h>
  22.  #include <speex/speex_jitter.h>
  23.  #include <speex/speex_preprocess.h>
  24. @@ -55,9 +67,213 @@
  25.  
  26.  #include <sched.h>
  27.  
  28. +/* begin playwav2.c paste */
  29. +
  30. +#if 0
  31. +#include <linux/msm_audio.h>
  32. +#else
  33. +/* ---------- linux/msm_audio.h -------- */
  34. +
  35. +#define AUDIO_IOCTL_MAGIC 'a'
  36. +
  37. +#define AUDIO_START        _IOW(AUDIO_IOCTL_MAGIC, 0, unsigned)
  38. +#define AUDIO_STOP         _IOW(AUDIO_IOCTL_MAGIC, 1, unsigned)
  39. +#define AUDIO_FLUSH        _IOW(AUDIO_IOCTL_MAGIC, 2, unsigned)
  40. +#define AUDIO_GET_CONFIG   _IOR(AUDIO_IOCTL_MAGIC, 3, unsigned)
  41. +#define AUDIO_SET_CONFIG   _IOW(AUDIO_IOCTL_MAGIC, 4, unsigned)
  42. +#define AUDIO_GET_STATS    _IOR(AUDIO_IOCTL_MAGIC, 5, unsigned)
  43. +#define AUDIO_ENABLE_AUDPP _IOW(AUDIO_IOCTL_MAGIC, 6, unsigned)
  44. +#define AUDIO_SET_ADRC     _IOW(AUDIO_IOCTL_MAGIC, 7, unsigned)
  45. +#define AUDIO_SET_EQ       _IOW(AUDIO_IOCTL_MAGIC, 8, unsigned)
  46. +#define AUDIO_SET_RX_IIR   _IOW(AUDIO_IOCTL_MAGIC, 9, unsigned)
  47. +
  48. +#define EQ_MAX_BAND_NUM 12
  49. +
  50. +#define ADRC_ENABLE  0x0001
  51. +#define ADRC_DISABLE 0x0000
  52. +#define EQ_ENABLE    0x0002
  53. +#define EQ_DISABLE   0x0000
  54. +#define IIR_ENABLE   0x0004
  55. +#define IIR_DISABLE  0x0000
  56. +
  57. +struct eq_filter_type
  58. +{
  59. +  int16_t gain;
  60. +  uint16_t freq;
  61. +  uint16_t type;
  62. +  uint16_t qf;
  63. +};
  64. +
  65. +struct eqalizer
  66. +{
  67. +  uint16_t bands;
  68. +  uint16_t params[132];
  69. +};
  70. +
  71. +struct rx_iir_filter
  72. +{
  73. +  uint16_t num_bands;
  74. +  uint16_t iir_params[48];
  75. +};
  76. +
  77. +
  78. +struct msm_audio_config
  79. +{
  80. +  uint32_t buffer_size;
  81. +  uint32_t buffer_count;
  82. +  uint32_t channel_count;
  83. +  uint32_t sample_rate;
  84. +  uint32_t codec_type;
  85. +  uint32_t unused[3];
  86. +};
  87. +
  88. +struct msm_audio_stats
  89. +{
  90. +  uint32_t out_bytes;
  91. +  uint32_t unused[3];
  92. +};
  93. +
  94. +/* Audio routing */
  95. +
  96. +#define SND_IOCTL_MAGIC 's'
  97. +
  98. +#define SND_MUTE_UNMUTED 0
  99. +#define SND_MUTE_MUTED   1
  100. +
  101. +struct msm_snd_device_config
  102. +{
  103. +  uint32_t device;
  104. +  uint32_t ear_mute;
  105. +  uint32_t mic_mute;
  106. +};
  107. +
  108. +#define SND_SET_DEVICE _IOW(SND_IOCTL_MAGIC, 2, struct msm_device_config *)
  109. +
  110. +#define SND_METHOD_VOICE 0
  111. +
  112. +#define SND_METHOD_VOICE_1 1
  113. +
  114. +struct msm_snd_volume_config
  115. +{
  116. +  uint32_t device;
  117. +  uint32_t method;
  118. +  uint32_t volume;
  119. +};
  120. +
  121. +#define SND_SET_VOLUME _IOW(SND_IOCTL_MAGIC, 3, struct msm_snd_volume_config *)
  122. +
  123. +/* Returns the number of SND endpoints supported. */
  124. +
  125. +#define SND_GET_NUM_ENDPOINTS _IOR(SND_IOCTL_MAGIC, 4, unsigned *)
  126. +
  127. +struct msm_snd_endpoint
  128. +{
  129. +  int id;                       /* input and output */
  130. +  char name[64];                /* output only */
  131. +};
  132. +
  133. +/* Takes an index between 0 and one less than the number returned by
  134. + * SND_GET_NUM_ENDPOINTS, and returns the SND index and name of a
  135. + * SND endpoint.  On input, the .id field contains the number of the
  136. + * endpoint, and on exit it contains the SND index, while .name contains
  137. + * the description of the endpoint.
  138. + */
  139. +
  140. +#define SND_GET_ENDPOINT _IOWR(SND_IOCTL_MAGIC, 5, struct msm_snd_endpoint *)
  141. +
  142. +#endif
  143. +/* ----------  -------- */
  144. +
  145. +static int
  146. +do_route_audio_rpc (uint32_t device, int ear_mute, int mic_mute)
  147. +{
  148. +  if (device == -1UL)
  149. +    return 0;
  150. +
  151. +  int fd;
  152. +
  153. +  printf ("rpc_snd_set_device(%d, %d, %d)\n", device, ear_mute, mic_mute);
  154. +
  155. +  fd = open ("/dev/msm_snd", O_RDWR);
  156. +  if (fd < 0)
  157. +    {
  158. +      perror ("Can not open snd device");
  159. +      return -1;
  160. +    }
  161. +  // RPC call to switch audio path
  162. +  /* rpc_snd_set_device(
  163. +   *     device,            # Hardware device enum to use
  164. +   *     ear_mute,          # Set mute for outgoing voice audio
  165. +   *                        # this should only be unmuted when in-call
  166. +   *     mic_mute,          # Set mute for incoming voice audio
  167. +   *                        # this should only be unmuted when in-call or
  168. +   *                        # recording.
  169. +   *  )
  170. +   */
  171. +  struct msm_snd_device_config args;
  172. +  args.device = device;
  173. +  args.ear_mute = ear_mute ? SND_MUTE_MUTED : SND_MUTE_UNMUTED;
  174. +  args.mic_mute = mic_mute ? SND_MUTE_MUTED : SND_MUTE_UNMUTED;
  175. +
  176. +  if (ioctl (fd, SND_SET_DEVICE, &args) < 0)
  177. +    {
  178. +      perror ("snd_set_device error.");
  179. +      close (fd);
  180. +      return -1;
  181. +    }
  182. +
  183. +  close (fd);
  184. +  return 0;
  185. +}
  186. +
  187. +static int
  188. +set_volume_rpc (uint32_t device, uint32_t method, uint32_t volume)
  189. +{
  190. +  int fd;
  191. +
  192. +  printf ("rpc_snd_set_volume(%d, %d, %d)\n", device, method, volume);
  193. +
  194. +  if (device == -1UL)
  195. +    return 0;
  196. +
  197. +  fd = open ("/dev/msm_snd", O_RDWR);
  198. +  if (fd < 0)
  199. +    {
  200. +      perror ("Can not open snd device");
  201. +      return -1;
  202. +    }
  203. +  /* rpc_snd_set_volume(
  204. +   *     device,            # Any hardware device enum, including
  205. +   *                        # SND_DEVICE_CURRENT
  206. +   *     method,            # must be SND_METHOD_VOICE to do anything useful
  207. +   *     volume,            # integer volume level, in range [0,5].
  208. +   *                        # note that 0 is audible (not quite muted)
  209. +   *  )
  210. +   * rpc_snd_set_volume only works for in-call sound volume.
  211. +   */
  212. +  struct msm_snd_volume_config args;
  213. +  args.device = device;
  214. +  args.method = method;
  215. +  args.volume = volume;
  216. +
  217. +  if (ioctl (fd, SND_SET_VOLUME, &args) < 0)
  218. +    {
  219. +      perror ("snd_set_volume error.");
  220. +      close (fd);
  221. +      return -1;
  222. +    }
  223. +  close (fd);
  224. +  return 0;
  225. +}
  226. +
  227. +static char *next;
  228. +static unsigned avail;
  229. +
  230. +/* end playwav2.c paste */
  231. +
  232.  #define MAX_MSG 1500
  233.  
  234. -#define SAMPLING_RATE 16000
  235. +#define SAMPLING_RATE 8000
  236.  #define FRAME_SIZE 320
  237.  
  238.  int main(int argc, char *argv[])
  239. @@ -72,7 +288,7 @@
  240.     int nfds;
  241.     struct pollfd *pfds;
  242.     SpeexPreprocessState *preprocess;
  243. -   AlsaDevice *audio_dev;
  244. +   /*AlsaDevice *audio_dev;*/
  245.     int tmp;
  246.  
  247.     if (argc != 5)
  248. @@ -118,16 +334,69 @@
  249.     }
  250.  
  251.     /* Setup audio device */
  252. -   audio_dev = alsa_device_open(argv[1], SAMPLING_RATE, 1, FRAME_SIZE);
  253. -   
  254. +   /* audio_dev = alsa_device_open(argv[1], SAMPLING_RATE, 1, FRAME_SIZE); */
  255. +
  256. +/* begin playwav2.c paste */
  257. +
  258. +  int fd = open ("/dev/msm_snd", O_RDWR);
  259. +  int mNumSndEndpoints;
  260. +
  261. +  if (fd >= 0)
  262. +    {
  263. +      int rc = ioctl (fd, SND_GET_NUM_ENDPOINTS,
  264. +                      &mNumSndEndpoints);
  265. +
  266. +      printf ("found %d snd endpoints\n", mNumSndEndpoints);
  267. +
  268. +      if (rc >= 0)
  269. +        {
  270. +          struct msm_snd_endpoint ept;
  271. +          int cnt;
  272. +
  273. +          for (cnt = 0; cnt < mNumSndEndpoints; cnt++)
  274. +            {
  275. +              ept.id = cnt;
  276. +              ioctl (fd, SND_GET_ENDPOINT, &ept);
  277. +              printf ("    %02d: %s / %d\n", cnt, ept.name, ept.id);
  278. +            }
  279. +        }
  280. +      else
  281. +        perror ("Could not retrieve number of MSM SND endpoints.");
  282. +      close (fd);
  283. +    }
  284. +  else
  285. +    perror ("Could not open MSM SND driver.");
  286. +
  287. +  printf ("Select device %d.\n", 0);
  288. +
  289. +//  do_route_audio_rpc (0, SND_MUTE_MUTED, SND_MUTE_MUTED);
  290. +  do_route_audio_rpc(0,SND_MUTE_UNMUTED,SND_MUTE_UNMUTED);
  291. +
  292. +
  293. +//  printf ("enable PP\n");
  294. +//  msm72xx_enable_audpp(ADRC_ENABLE | EQ_ENABLE | IIR_ENABLE);
  295. +
  296. +  printf ("Set master volume to %d.\n", 5);
  297. +#if 0
  298. +  set_volume_rpc (SND_DEVICE_HANDSET, SND_METHOD_VOICE, vol);
  299. +  set_volume_rpc (SND_DEVICE_SPEAKER, SND_METHOD_VOICE, vol);
  300. +  set_volume_rpc (SND_DEVICE_BT, SND_METHOD_VOICE, vol);
  301. +  set_volume_rpc (SND_DEVICE_HEADSET, SND_METHOD_VOICE, vol);
  302. +#endif
  303. +
  304. +  /* dev=0xd ? */
  305. +  set_volume_rpc (0xd, SND_METHOD_VOICE_1, 5);
  306. +
  307. +/* end playwav2.c paste */
  308. +
  309.     /* Setup the encoder and decoder in wideband */
  310.     void *enc_state, *dec_state;
  311. -   enc_state = speex_encoder_init(&speex_wb_mode);
  312. +   enc_state = speex_encoder_init(&speex_nb_mode);
  313.     tmp = 8;
  314.     speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
  315.     tmp = 2;
  316.     speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);
  317. -   dec_state = speex_decoder_init(&speex_wb_mode);
  318. +   dec_state = speex_decoder_init(&speex_nb_mode);
  319.     tmp = 1;
  320.     speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &tmp);
  321.     SpeexBits enc_bits, dec_bits;
  322. @@ -145,9 +414,14 @@
  323.     int recv_started=0;
  324.    
  325.     /* Setup all file descriptors for poll()ing */
  326. -   nfds = alsa_device_nfds(audio_dev);
  327. +   /* nfds = alsa_device_nfds(audio_dev); */
  328. +   nfds = 2;
  329.     pfds = malloc(sizeof(*pfds)*(nfds+1));
  330. -   alsa_device_getfds(audio_dev, pfds, nfds);
  331. +   /* alsa_device_getfds(audio_dev, pfds, nfds); */
  332. +   pfds[0].fd = open("/dev/msm_pcm_in", O_RDWR);
  333. +   pfds[0].events = POLLIN;
  334. +   pfds[1].fd = open("/dev/msm_pcm_out", O_RDWR);
  335. +   pfds[1].events = POLLOUT;
  336.     pfds[nfds].fd = sd;
  337.     pfds[nfds].events = POLLIN;
  338.  
  339. @@ -164,8 +438,104 @@
  340.     preprocess = speex_preprocess_state_init(FRAME_SIZE, SAMPLING_RATE);
  341.     speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo_state);
  342.    
  343. -   alsa_device_start(audio_dev);
  344. -   
  345. +   /* alsa_device_start(audio_dev); */
  346. +
  347. +/* begin playwav2.c paste */
  348. +
  349. +//  unsigned char buf[8192];
  350. +  unsigned char buf[320]
  351. +  struct msm_audio_config cfg;
  352. +  unsigned sz;
  353. +  unsigned total = 0;
  354. +
  355. +  if (pfds[0].fd < 0)
  356. +    {
  357. +      perror ("cannot open msm_pcm_in");
  358. +      return -1;
  359. +    }
  360. +
  361. +  /* config change should be a read-modify-write operation */
  362. +  if (ioctl (pfds[0].fd, AUDIO_GET_CONFIG, &cfg))
  363. +    {
  364. +      perror ("cannot read write audio config");
  365. +      return -1;
  366. +    }
  367. +
  368. +  cfg.channel_count = 1;
  369. +  cfg.sample_rate = 8000;
  370. +  cfg.buffer_size = 320;
  371. +  if (ioctl (pfds[0].fd, AUDIO_SET_CONFIG, &cfg))
  372. +    {
  373. +      perror ("cannot write write audio config");
  374. +      return -1;
  375. +    }
  376. +
  377. +  if (ioctl (pfds[0].fd, AUDIO_GET_CONFIG, &cfg))
  378. +    {
  379. +      perror ("cannot read write audio config");
  380. +      return -1;
  381. +    }
  382. +
  383. +  sz = cfg.buffer_size;
  384. +  fprintf (stderr, "write buffer size %d x %d\n", sz, cfg.buffer_count);
  385. +  if (sz > sizeof (buf))
  386. +    {
  387. +      fprintf (stderr, "write buffer size %d too large\n", sz);
  388. +      return -1;
  389. +    }
  390. +
  391. +  if (ioctl (pfds[0].fd, AUDIO_START, 0))
  392. +    {
  393. +      perror ("cannot start write audio");
  394. +      return -1;
  395. +    }
  396. +
  397. +  struct msm_audio_config config;
  398. +  struct msm_audio_stats stats;
  399. +  unsigned sz2;
  400. +  char buf2[320];
  401. +
  402. +  if (pfds[1].fd < 0)
  403. +    {
  404. +      perror ("pcm_play: cannot open audio device");
  405. +      return -1;
  406. +    }
  407. +
  408. +  if (ioctl (pfds[1].fd, AUDIO_GET_CONFIG, &config))
  409. +    {
  410. +      perror ("could not get read config");
  411. +      return -1;
  412. +    }
  413. +
  414. +  config.channel_count = 1;
  415. +  config.sample_rate = 8000;
  416. +  config.buffer_size = 320;
  417. +  if (ioctl (pfds[1].fd, AUDIO_SET_CONFIG, &config))
  418. +    {
  419. +      perror ("could not set read config");
  420. +      return -1;
  421. +    }
  422. +  sz2 = config.buffer_size;
  423. +  if (sz2 > sizeof (buf2))
  424. +    {
  425. +      fprintf (stderr, "read buffer too big\n");
  426. +      return -1;
  427. +    }
  428. +
  429. +/*  fprintf (stderr, "prefill\n");
  430. +  for (n = 0; n < config.buffer_count; n++)
  431. +    {
  432. +      if (fill (buf, sz, cookie))
  433. +        break;
  434. +      if (write (afd, buf, sz) != sz)
  435. +        break;
  436. +    } */
  437. +
  438. +  ioctl (pfds[1].fd, AUDIO_START, 0);
  439. +
  440. +
  441. +/* end playwav2.c paste */
  442. +
  443.     /* Infinite loop on capture, playback and receiving packets */
  444.     while (1)
  445.     {
  446. @@ -188,8 +558,8 @@
  447.  
  448.        }
  449.        /* Ready to play a frame (playback) */
  450. -      if (alsa_device_playback_ready(audio_dev, pfds, nfds))
  451. -      {
  452. +//      if (alsa_device_playback_ready(audio_dev, pfds, nfds))
  453. +//      {
  454.           short pcm[FRAME_SIZE];
  455.           if (recv_started)
  456.           {
  457. @@ -200,18 +570,24 @@
  458.                 pcm[i] = 0;
  459.           }
  460.           /* Playback the audio and reset the echo canceller if we got an underrun */
  461. -         if (alsa_device_write(audio_dev, pcm, FRAME_SIZE))
  462. +//         if (alsa_device_write(audio_dev, pcm, FRAME_SIZE))
  463. +#if 0
  464. +      if (ioctl (pfds[1].fd, AUDIO_GET_STATS, &stats) == 0)
  465. +        fprintf (stderr, "%10d\n", stats.out_bytes);
  466. +#endif
  467. +         if (write (pfds[1].fd, pcm, FRAME_SIZE))
  468.              speex_echo_state_reset(echo_state);
  469.           /* Put frame into playback buffer */
  470.           speex_echo_playback(echo_state, pcm);
  471. -      }
  472. +//      }
  473.        /* Audio available from the soundcard (capture) */
  474. -      if (alsa_device_capture_ready(audio_dev, pfds, nfds))
  475. -      {
  476. +//      if (alsa_device_capture_ready(audio_dev, pfds, nfds))
  477. +//      {
  478.           short pcm[FRAME_SIZE], pcm2[FRAME_SIZE];
  479.           char outpacket[MAX_MSG];
  480.           /* Get audio from the soundcard */
  481. -         alsa_device_read(audio_dev, pcm, FRAME_SIZE);
  482. +//         alsa_device_read(audio_dev, pcm, FRAME_SIZE);
  483. +         read (pfds[0].fd, pcm, FRAME_SIZE);
  484.          
  485.           /* Perform echo cancellation */
  486.           speex_echo_capture(echo_state, pcm, pcm2);
  487. @@ -240,7 +616,7 @@
  488.              close(sd);
  489.              exit(1);
  490.           }
  491. -      }
  492. +//      }
  493.        
  494.  
  495.     }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

fantasy-obligation
fantasy-obligation
fantasy-obligation