PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1KB

  1. % Talkie library
  2. % Copyright 2011 Peter Knight
  3. % This code is released under GPLv2 license.
  4. %
  5. % Main LPC mapping code.
  6. % Converts WAV file into LPC model parameters.
  7. clear
  8. params = [];
  9. % LPC10 encoder
  10. % Read source file
  11. [a,sampleRate, bitDepth] = wavread('TomsDiner8.wav');
  12. b=a.*0; % Output buffer, matching size
  13. % LPC10 frame is 25ms at 8000Hz
  14. frameTime = 0.025;
  15. frameSamples = sampleRate * frameTime;
  16. W = 2*frameSamples; % Window is twice as long as frame, to allow for windowing overlaps
  17. % Precalculate hanning window, length 2 frames
  18. hannWindow = transpose(0.5*(1-cos(2*pi*(0:(W-1))/(W-1))));
  19. % Precalculate phases for 1Hz
  20. phase = (1:W)*2*pi/sampleRate;
  21. lpcOrder=10;
  22. lena = length(a);
  23. for frameStart = 1:W/2:(lena-W)
  24. % Window chunk of input
  25. frameChunk = a(frameStart:(frameStart+W-1),1);
  26. frameWindowed = (frameChunk .* hannWindow);
  27. % Measure energy
  28. frameEnergy = sqrt(mean(frameWindowed.*frameWindowed));
  29. % Measure pitch
  30. pitch = 235; % Mid point of Suzanne Vega's pitch range
  31. [pitch,pitchScore] = pitchRefine(frameWindowed,pitch,100,sampleRate);
  32. [pitch,pitchScore] = pitchRefine(frameWindowed,pitch,30,sampleRate);
  33. [pitch,pitchScore] = pitchRefine(frameWindowed,pitch,10,sampleRate);
  34. [pitch,pitchScore] = pitchRefine(frameWindowed,pitch,3,sampleRate);
  35. % Consonant detection
  36. if (pitchScore/frameEnergy > 0.1)
  37. isVoiced = 1;
  38. else
  39. isVoiced = 0;
  40. pitch = 0;
  41. end
  42. % Calculate LPC coefficients
  43. r = autocorrelate(frameWindowed,lpcOrder+1);
  44. [k,g] = levinsonDurbin(r,lpcOrder);
  45. if isVoiced==0
  46. g = 0.1*g;
  47. end
  48. [frameStart/lena,g] % Show status
  49. % Quantise to match bit coding
  50. [pitch,g,k,frameBits] = lpcQuantise(pitch,g,k);
  51. params = vertcat(params,frameBits);
  52. % Synthesise from parameters
  53. d = lpcSynth(pitch,g,k,W,lpcOrder,sampleRate);
  54. d = d .* hannWindow;
  55. % Write back pitch to output wav
  56. b(frameStart:(frameStart+2*frameSamples-1)) = b(frameStart:(frameStart+2*frameSamples-1)) + d;
  57. end
  58. b = transpose(b);
  59. wavwrite(b,8000,16,'TomsDinerPitch.wav');
  60. csvwrite('tomsDinerStream.csv',params);