github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/audio/benchmark/gen.go (about)

     1  package benchmark_audio
     2  
     3  type Sampler func(buf *Buffer, generate func())
     4  
     5  // Generete fills audio left, right channels and rest with 0-s
     6  func Generate_Dynamic(buf *Buffer, generate func() float32) {
     7  	nchan := int(buf.ChannelCount)
     8  	maxg := 2
     9  	if maxg > nchan {
    10  		maxg = nchan
    11  	}
    12  	for i := 0; i < len(buf.Data); i += nchan {
    13  		sample := generate()
    14  		for k := 0; k < maxg; k++ {
    15  			buf.Data[i+k] = sample
    16  		}
    17  		for k := maxg; k < nchan; k++ {
    18  			buf.Data[i+k] = 0
    19  		}
    20  	}
    21  }
    22  
    23  // Generete fills audio channels
    24  func Generate_Switch(buf *Buffer, generate func() float32) {
    25  	switch buf.ChannelCount {
    26  	case 1:
    27  		for i := 0; i < len(buf.Data); i++ {
    28  			sample := generate()
    29  			buf.Data[i] = sample
    30  		}
    31  	case 2:
    32  		for i := 0; i < len(buf.Data)-1; i += 2 {
    33  			sample := generate()
    34  			buf.Data[i] = sample
    35  			buf.Data[i+1] = sample
    36  		}
    37  	case 5:
    38  		for i := 0; i < len(buf.Data)-5; i += 5 {
    39  			sample := generate()
    40  			buf.Data[i] = sample
    41  			buf.Data[i+1] = sample
    42  			buf.Data[i+2] = 0
    43  			buf.Data[i+3] = 0
    44  			buf.Data[i+4] = 0
    45  		}
    46  	case 7:
    47  		for i := 0; i < len(buf.Data)-6; i += 7 {
    48  			sample := generate()
    49  			buf.Data[i] = sample
    50  			buf.Data[i+1] = sample
    51  			buf.Data[i+2] = 0
    52  			buf.Data[i+3] = 0
    53  			buf.Data[i+4] = 0
    54  			buf.Data[i+5] = 0
    55  			buf.Data[i+6] = 0
    56  		}
    57  	}
    58  
    59  }
    60  
    61  // Sawtooth
    62  func Sawtooth_Dynamic(buf *Buffer, sampleRate uint32, hz float32) {
    63  	phase := float32(0.0)
    64  	phaseSpeed := 2 * hz / float32(sampleRate)
    65  	Generate_Dynamic(buf, func() float32 {
    66  		phase += phaseSpeed
    67  		if phase > 1 {
    68  			phase -= 2.0
    69  		}
    70  		return phase
    71  	})
    72  }
    73  
    74  // Sawtooth
    75  func Sawtooth_Switch(buf *Buffer, sampleRate uint32, hz float32) {
    76  	phase := float32(0.0)
    77  	phaseSpeed := 2 * hz / float32(sampleRate)
    78  	Generate_Switch(buf, func() float32 {
    79  		phase += phaseSpeed
    80  		if phase > 1 {
    81  			phase -= 2.0
    82  		}
    83  		return phase
    84  	})
    85  }
    86  
    87  // Sawtooth inlined
    88  func Sawtooth_Baseline_Ch1(buf Buffer_Ch1, sampleRate uint32, hz float32) {
    89  	phase := float32(0.0)
    90  	phaseSpeed := 2 * hz / float32(sampleRate)
    91  	for i := range buf {
    92  		buf[i][0] = phase
    93  		phase += phaseSpeed
    94  		if phase > 1 {
    95  			phase -= 2.0
    96  		}
    97  	}
    98  }
    99  
   100  // Sawtooth inlined
   101  func Sawtooth_Baseline_Ch2(buf Buffer_Ch2, sampleRate uint32, hz float32) {
   102  	phase := float32(0.0)
   103  	phaseSpeed := 2 * hz / float32(sampleRate)
   104  	for i := range buf {
   105  		buf[i][0] = phase
   106  		buf[i][1] = phase
   107  
   108  		phase += phaseSpeed
   109  		if phase > 1 {
   110  			phase -= 2.0
   111  		}
   112  	}
   113  }
   114  
   115  // Sawtooth inlined
   116  func Sawtooth_Baseline_Ch5(buf Buffer_Ch5, sampleRate uint32, hz float32) {
   117  	phase := float32(0.0)
   118  	phaseSpeed := 2 * hz / float32(sampleRate)
   119  	for i := range buf {
   120  		buf[i][0] = phase
   121  		buf[i][1] = phase
   122  
   123  		buf[i][2] = 0
   124  		buf[i][3] = 0
   125  		buf[i][4] = 0
   126  
   127  		phase += phaseSpeed
   128  		if phase > 1 {
   129  			phase -= 2.0
   130  		}
   131  	}
   132  }
   133  
   134  // Sawtooth inlined
   135  func Sawtooth_Baseline_Ch7(buf Buffer_Ch7, sampleRate uint32, hz float32) {
   136  	phase := float32(0.0)
   137  	phaseSpeed := 2 * hz / float32(sampleRate)
   138  	for i := range buf {
   139  		buf[i][0] = phase
   140  		buf[i][1] = phase
   141  
   142  		buf[i][2] = 0
   143  		buf[i][3] = 0
   144  		buf[i][4] = 0
   145  		buf[i][5] = 0
   146  		buf[i][6] = 0
   147  
   148  		phase += phaseSpeed
   149  		if phase > 1 {
   150  			phase -= 2.0
   151  		}
   152  	}
   153  }