Evolutionary pressure suggests that the spike-based code in the sensory nervous system should satisfy two opposing constraints: 1) minimize signal distortion in the encoding process (i.e., maintain fidelity) by keeping the average spike rate as high as possible, and 2) minimize the metabolic load on the neuron by keeping the average spike rate as low as possible. We hypothesize that selective pressure has shaped the biophysics of a neuron to satisfy these conflicting demands. An energy-fidelity trade-off can be obtained through a constrained optimization process that achieves the lowest signal distortion for a given constraint on the spike rate. We derive the asymptotically optimal average-energy-constrained neuronal source code and show that it leads to a dynamic threshold that functions as an internal decoder (reconstruction filter) and adapts a spike-firing threshold so that spikes are emitted only when the coding error reaches this threshold. A stochastic extension is obtained by adding internal noise (dithering, or stochastic resonance) to the spiking threshold. We show that the source-coding neuron model i) reproduces experimentally observed spike-times in response to a stimulus, and ii) reproduces the serial correlations in the observed sequence of inter-spike intervals, using data from a peripheral sensory neuron and a central (cortical) somatosensory neuron. Finally, we show that the spike-timing code, although a temporal code, is in the limit of high firing rates an instantaneous rate code and accurately predicts the peri-stimulus time histogram (PSTH). We conclude by suggesting possible biophysical (ionic) mechanisms for this coding scheme.