pitch.cpp
1 /* 2 Copyright (C) 1994-1995 Apogee Software, Ltd. 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License 6 as published by the Free Software Foundation; either version 2 7 of the License, or (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 13 See the GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 19 */ 20 /********************************************************************** 21 module: PITCH.C 22 23 author: James R. Dose 24 date: June 14, 1993 25 26 Routines for pitch scaling. 27 28 (c) Copyright 1993 James R. Dose. All Rights Reserved. 29 **********************************************************************/ 30 31 #include "compat.h" 32 #include "pitch.h" 33 34 #define MAXDETUNE 50 35 36 static uint32_t PitchTable[12][MAXDETUNE]; 37 38 39 /*--------------------------------------------------------------------- 40 Function: PITCH_Init 41 42 Initializes pitch table. 43 ---------------------------------------------------------------------*/ 44 45 46 void PITCH_Init(void) 47 { 48 for (int note = 0; note < 12; note++) 49 for (int detune = 0; detune < MAXDETUNE; detune++) 50 PitchTable[note][detune] = (uint32_t) (65536.f * powf(2.f, (note * MAXDETUNE + detune) / (12.f * MAXDETUNE))); 51 } 52 53 54 /*--------------------------------------------------------------------- 55 Function: PITCH_GetScale 56 57 Returns a fixed-point value to scale number the specified amount. 58 ---------------------------------------------------------------------*/ 59 60 uint32_t PITCH_GetScale(int const pitchoffset) 61 { 62 static bool bInitialized; 63 64 if (!bInitialized) 65 { 66 PITCH_Init(); 67 bInitialized = true; 68 } 69 70 if (pitchoffset == 0) 71 return PitchTable[0][0]; 72 73 int noteshift = pitchoffset % 1200; 74 75 if (noteshift < 0) 76 noteshift += 1200; 77 78 int const note = noteshift / 100; 79 int const detune = (noteshift % 100) / (100 / MAXDETUNE); 80 int const oshift = (pitchoffset - noteshift) / 1200; 81 auto const &scale = PitchTable[note][detune]; 82 83 return (oshift < 0) ? (scale >> -oshift) : (scale << oshift); 84 }