2
0

lspsync.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. lspsync.c
  3. David Rowe 24 May 2012
  4. Attempt at using LSP information to provide frame sync. If we have
  5. correct frame alignment, LSPs will not need sorting.
  6. However this method as tested appears unreliable, often several
  7. sync positions per frame are found, even with a F=10 memory. For
  8. F=6, about 87% relaible. This might be useful if combined with a
  9. another sync method, for example a single alternating sync bit per
  10. frame.
  11. */
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "codec2.h"
  16. #include "defines.h"
  17. #include "quantise.h"
  18. #define F 6 /* look at LSP ordering in F-1 frames */
  19. #define CORRECT_OFFSET 10 /* LSPs start 10 bits int frame qt 2400 bit/s */
  20. static int check_candidate(char bits[], int offset)
  21. {
  22. int i;
  23. int lsp_indexes[LPC_ORD];
  24. float lsps[LPC_ORD];
  25. unsigned int nbit = offset;
  26. int swaps;
  27. for(i=0; i<LSP_SCALAR_INDEXES; i++) {
  28. lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
  29. }
  30. decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD);
  31. swaps = check_lsp_order(lsps, LPC_ORD);
  32. return swaps;
  33. }
  34. int main(int argc, char *argv[]) {
  35. struct CODEC2 *c2;
  36. int i,offset, nsamples, nbits, nbytes, frames;
  37. short *speech;
  38. char *bits;
  39. FILE *fin;
  40. int swaps, pass, fail, match;
  41. c2 = codec2_create(CODEC2_MODE_2400);
  42. nsamples = codec2_samples_per_frame(c2);
  43. nbits = codec2_bits_per_frame(c2);
  44. nbytes = nbits/8;
  45. speech = (short*)malloc(nsamples*sizeof(short));
  46. /* keep FRAMES frame memory of bit stream */
  47. bits = (char*)malloc(F*nbytes*sizeof(char));
  48. for(i=0; i<F*nbytes; i++)
  49. bits[i] = 0;
  50. fin = fopen("../raw/hts1a.raw", "rb");
  51. assert(fin != NULL);
  52. match = pass = fail = frames = 0;
  53. /* prime memeory with first frame to ensure we don't start
  54. checking until we have two frames of coded bits */
  55. fread(speech, sizeof(short), nsamples, fin);
  56. frames++;
  57. codec2_encode(c2, &bits[(F-2)*nbytes], speech);
  58. /* OK start looking for correct frame offset */
  59. while(fread(speech, sizeof(short), nsamples, fin) == nsamples) {
  60. frames++;
  61. codec2_encode(c2, &bits[(F-1)*nbytes], speech);
  62. for(offset=0; offset<nbits; offset++) {
  63. swaps = check_candidate(bits, offset);
  64. if (swaps == 0) {
  65. /* OK found a candidate .. lets check a F-1 frames in total */
  66. for(i=0; i<(F-1); i++)
  67. swaps += check_candidate(bits, offset + nbits*i);
  68. if (swaps == 0) {
  69. printf("frame %d offset: %d swaps: %d\n", frames, offset, swaps);
  70. match++;
  71. if (offset == CORRECT_OFFSET)
  72. pass++;
  73. else
  74. fail++;
  75. }
  76. }
  77. }
  78. /* update F frame memory of bits */
  79. for(i=0; i<nbytes*(F-1); i++)
  80. bits[i] = bits[i+nbytes];
  81. }
  82. fclose(fin);
  83. free(speech);
  84. free(bits);
  85. codec2_destroy(c2);
  86. printf("passed %f %%\n", (float)pass*100.0/match);
  87. return 0;
  88. }