download MKPartialsWaveshapingTable.m
Language: ObjectiveC
Copyright: (c) 1988-1992, NeXT Computer, Inc. (c) 1999-2005, The MusicKit Project. (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT (c) 1994 Stanford University
LOC: 131
Project Info
The MusicKit(musickit)
Server: SourceForge
Type: cvs
...sicKit\Frameworks\MusicKit\
   _ArielQP.h
   _DSPMK.h
   _error.h
   _midi.h
   _MKAppProxy.h
   _MKAppProxy.m
   _MKNameTable.h
   _MKNameTable.m
   _MKParameter.h
   _MTCHelper.h
   _MTCHelper.m
   _musickit.h
   _noteRecorder.h
   _OrchloopbeginUG.h
   _OrchloopbeginUG.m
   _ParName.h
   _ParName.m
   _scorefile.h
   _ScorefileVar.h
   _ScorefileVar.m
   _SharedSynthInfo.h
   _SharedSynthInfo.m
   _synthElementMethods.m
   _time.h
   ArielQP.h
   ArielQP.m
   classFuncs.h
   ConductorPrivate.h
   dsp_types.h
   DSPSerialPortDevice.h
   dspwrap.h
   EnvelopePrivate.h
   equalTempered.m
   errors.h
   fastFFT.c
   fastFFT.h
   GNUmakefile.postamble
   GNUmakefile.preamble.in
   InstrumentPrivate.h
   keynums.h
   libMusicKit.def
   Localized.strings
   make.sh
   Makefile.postamble
   Makefile.preamble
   midi_spec.h
   midifile.h
   midifile.m
   MidiPrivate.h
   midiTranslation.h
   MKConductor.h
   MKConductor.m
   MKConductorDelegate.h
   MKDeviceStatus.h
   MKEnvelope.h
   MKEnvelope.m
   MKFilePerformer.h
   MKFilePerformer.m
   MKFileWriter.h
   MKFileWriter.m
   MKInstrument.h
   MKInstrument.m
   MKMidi.h
   MKMidi.m
   MKMixerInstrument.h
   MKMixerInstrument.m
   MKMTCPerformer.h
   MKMTCPerformer.m
   MKNote.h
   MKNoteFilter.h
   MKNoteFilter.m
   MKNoteReceiver.h
   MKNoteReceiver.m
   MKNoteSender.h
   MKNoteSender.m
   MKOrchestra.h
   MKPart.m
   MKPartials.h
   MKPartials.m
   ...tialsWaveshapingTable.m
   MKPartPerformer.h
   MKPartPerformer.m
   MKPartRecorder.h
   MKPartRecorder.m
   MKPatch.h
   MKPatch.m
   MKPatchConnection.h
   MKPatchConnection.m
   MKPatchEntry.h
   MKPatchEntry.m
   MKPatchTemplate.h
   MKPatchTemplate.m
   MKPerformer.h
   MKPerformer.m
   MKPerformerDelegate.h
   MKPlugin.h
   MKSamplerInstrument.h
   MKSamplerInstrument.m
   MKSamples.h
   MKScore.h
   MKScore.m
   MKScorefileObject.h
   MKScorefilePerformer.h
   MKScorefilePerformer.m
   MKScorefileWriter.m
   MKScorePerformer.h
   MKScorePerformer.m
   MKScoreRecorder.h
   MKScoreRecorder.m
   MKSynthData.h
   MKSynthData.m
   MKSynthInstrument.h
   MKSynthPatch.h
   MKTimbre.h
   MKTimbre.m
   MKTuningSystem.h
   MKTuningSystem.m
   MKUnitGenerator.h
   MKUnitGenerator.m
   MKWaveTable.h
   MKWaveTable.m
   mtcMidi.m
   mtcMidiPrivate.m
   MusicKit-Info.plist
   MusicKit.h
   MusicKitConfig.h.in
   names.h
   noDVal.h
   noteDispatcherMethods.m
   NotePrivate.h
   noteRecorderCFuncs.m
   noteRecorderMethods.m
   orch.h
   OrchestraPrivate.h
   OrchloopbeginUG.h
   OrchloopbeginUG.m
   orchloopbeginUGInclude.m
   params.h
   parNames.m
   partialsDBInclude.m
   PartialsPrivate.h
   PartPerformerPrivate.h
   PartPrivate.h
   PartRecorderPrivate.h
   PatchTemplatePrivate.h
   PB.project
   PerformerPrivate.h
   pitches.h
   platform.make
   ...efilePerformerPrivate.h
   ScorePerformerPrivate.h
   ScorePrivate.h
   ScoreRecorderPrivate.h
   SynthDataPrivate.h
   synthElementCFuncs.m
   synthElementMethods.m
   SynthInstrumentPrivate.h
   SynthPatchList.h
   SynthPatchList.m
   SynthPatchPrivate.h
   timetagInclude.m
   timeunits.h
   tokenNames.m
   tokens.h
   trigonometry.c
   trigonometry.h
   TuningSystemPrivate.h
   UnitGeneratorPrivate.h
   win32-def.top
   writeBinary.m
   writeMidi.m
   writeScore.m

/*
  $Id: MKPartialsWaveshapingTable.m,v 1.6 2005/07/20 06:59:52 leighsmith Exp $
  Defined In: The MusicKit

  Description:
  Original Author: David Jaffe

  Copyright (c) 1988-1992, NeXT Computer, Inc.
  Portions Copyright (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
  Portions Copyright (c) 1994 Stanford University
  Portions Copyright (c) 1999-2005, The MusicKit Project.
*/
#import "_musickit.h"
#import "PartialsPrivate.h"

@implementation MKPartials(WaveshapingTable)

- (DSPDatum *) dataDSPAsWaveshapingTableLength: (unsigned int) aLength 
					 scale: (double) aScaling
{
    if ((tableType != MK_waveshapingTable) || 
	(length != aLength) || (scaling != aScaling) || (length == 0))
	if (![self fillWaveshapingTableLength:aLength scale:aScaling])
	    return NULL;
    if (!dataDSP && dataDouble) {
	_MK_MALLOC(dataDSP, DSPDatum, length);
	if (!dataDSP)
	    return NULL;
	_MKDoubleToFix24Array (dataDouble, dataDSP, length);
    } 
    return dataDSP;
}

- (double *)dataDoubleAsWaveshapingTableLength:(unsigned int)aLength scale:(double)aScaling
{  
   if ((tableType != MK_waveshapingTable) || 
       (length != aLength) || (scaling != aScaling) || (length == 0))
     if (![self fillWaveshapingTableLength:aLength scale:aScaling])
       return NULL;
   if (!dataDouble && dataDSP) {
       _MK_MALLOC (dataDouble, double, length);
       if (!dataDouble)
	   return NULL;
       _MKFix24ToDoubleArray (dataDSP, dataDouble, length);
   } 
   return dataDouble;
}

- (DSPDatum *)dataDSPAsWaveshapingTable
{
    return [self dataDSPAsWaveshapingTableLength:length scale:scaling];
}

- (double *)dataDoubleAsWaveshapingTable
{
    return [self dataDoubleAsWaveshapingTableLength:length scale:scaling];
}

- (DSPDatum *)dataDSPAsWaveshapingTableLength:(int)aLength
{
    return [self dataDSPAsWaveshapingTableLength:aLength scale:scaling];
}

- (double *)dataDoubleAsWaveshapingTableLength:(int)aLength
{
    return [self dataDoubleAsWaveshapingTableLength:aLength scale:scaling];
}

- (DSPDatum *)dataDSPAsWaveshapingTableScale:(double)aScaling
{
    return [self dataDSPAsWaveshapingTableLength:length scale:aScaling];
}

- (double *)dataDoubleAsWaveshapingTableScale:(double)aScaling
{
    return [self dataDoubleAsWaveshapingTableLength:length scale:aScaling];
}

/* This is PROGRAM 3 from the LeBrun article -- DAJ */
static void makeshape(double *F, double *Hk, int Lf, int Lh)
    /* Fis the resulting shaping function.  Lf is its length.
       Hk is the list of harmonic numbers.   Lh is its length.
       */
{
    int I, K;
    double X, Tn, Tn1, Tn2;

    for (I = 0; I < Lf; I++) {
	X = 2 * ((double)I/(double)(Lf-1))-1; /* Map to [-1:1] */
	F[I] = 0;
	Tn = 1;   /* Initialize inductive definition */
	Tn1 = X;
	for (K=0; K<=Lh; K++) {
	    F[I] += Hk[K] * Tn;
	    Tn2 = Tn1;
	    Tn1 = Tn;
	    Tn = 2 * X * Tn1 - Tn2;
	}
    }
}

#define DEFAULT_WAVESHAPING_TABLE_LENGTH 129

#define CHEBYCHEV_ERROR \
NSLocalizedStringFromTableInBundle(@"Waveshaping harmonics must be integers.", _MK_ERRTAB, _MKErrorBundle(), "This error occurs if an application asks for an inharmonic waveshaping partial.")

- fillWaveshapingTableLength: (unsigned int) aLength scale: (double) aScaling 
{
    double diff;
    int j, k, i;
    unsigned highestFreqRatio;  
    double *Hk;
    BOOL isEven;

    if (!ampRatios || !freqRatios || (partialCount <= 0))
	return nil;
    if (aLength == 0) {
	if (length == 0)
	    aLength = DEFAULT_WAVESHAPING_TABLE_LENGTH;
	else
	    aLength = length;
    }
    isEven = (aLength % 2 == 0);
    tableType = MK_waveshapingTable;
    if (!dataDouble || (length != aLength)) {
    	if (dataDouble) {
	    free(dataDouble); 
	    dataDouble = NULL;
	}
	_MK_CALLOC(dataDouble, double, aLength);
    }
    length = aLength;
    if (dataDSP) {
	free(dataDSP);
	dataDSP = NULL;
    } 
    /* Leave it to access method to fill dataDSP. */

    /* First figure the highest frequency ratio */
    highestFreqRatio = [self highestFreqRatio];

    /* Now allocate an array from 0 to highestFreqRatio */
    _MK_MALLOC(Hk, double, (highestFreqRatio + 1));
    memset(Hk, 0, (highestFreqRatio + 1) * sizeof(double));
    /* Now fill it with amplitudes.  */
    for (i = 0; i < partialCount; i++) {
	if (ampRatios[i] == 0.0) /* Necessary--see highestFreqRatio */
	    continue;
	j = (int)freqRatios[i];
	diff = freqRatios[i]-j;
	if (ABS(diff) > .0001) /* Floating point compare */ 
	    MKErrorCode(MK_musicKitErr,CHEBYCHEV_ERROR);
	k = j % 4;   /* Perform signification algorithm described in LeBrun 
		      * The idea is that you set Hk[2], [3], [6], [7], ... to
		      * negative amplitude as a way of making the index have
		      * a bit less effect on amplitude.  It's supposed to be
		      * ad hoc, but useful. (DAJ)
		      */
	if (k == 2 || k == 3)
	    Hk[j] = -ampRatios[i];
	else
	    Hk[j] = ampRatios[i];
    }
    /* Bill Schottstaedt's version of this instrument normalizes the spectrum
     * at this point.  I'm not sure that's necessary, so I don't do it. (DAJ) 
     */
    if (isEven) 
	aLength -= 1;
    makeshape(dataDouble, Hk,aLength, highestFreqRatio);
    if (isEven) /* Copy last point in this case */
	dataDouble[length-1] = dataDouble[aLength-1];
    scaling = aScaling;
    [self _normalize];
    return self;
}

@end

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us