The stellar initial mass function (IMF) is playing a critical role in the history of our universe. We propose a theory that is based solely on local processes, namely the dust opacity limit, the tidal forces and the properties of the collapsing gas envelope. The idea is that the final mass of the central object is determined by the location of the nearest fragments, which accrete the gas located further away, preventing it to fall onto the central object. To estimate the relevant statistics in the neighbourhood of an accreting protostar, we perform high resolution numerical simulations. We also use these simulations to further test the idea that fragmentation in the vicinity of an existing protostar is determinant in setting the peak of the stellar mass spectrum. We develop an analytical model, which is based on a statistical counting of the turbulent density fluctuations, generated during the collapse, that are at least equal to the mass of the first hydrostatic core, and sufficiently important to supersede tidal and pressure forces to be self-gravitating. The analytical mass function presents a peak located at roughly 10 times the mass of the first hydrostatic core in good agreement with the numerical simulations. Since the physical processes involved are all local, i.e. occurs at scales of a few 100 AU or below, and do not depend on the gas distribution at large scale and global properties such as the mean Jeans mass, the mass spectrum is expected to be relatively universal.